Esempio n. 1
0
        private void WriteProperties(PropertyDescriptor metadata, object entity, TypeDescriptor entityDescriptor, XmlTextWriter writer, XmlSerializerContext context, bool writeAttribute)
        {
            IXmlConverter      propConverter;
            PropertyDescriptor desc;
            object             value;
            bool mustWrite;

            //agrego la propiedades
            for (int i = 0; i < entityDescriptor.Properties.Count; i++)
            {
                desc  = entityDescriptor.Properties[i];
                value = desc.Metadata.GetValue(entity);

                if (value != null)
                {
                    Type entityType = value.GetType();

                    if (writeAttribute)
                    {
                        mustWrite = entityDescriptor.IsAttributeProperty(desc, entityType);
                    }
                    else
                    {
                        mustWrite = entityDescriptor.IsElementProperty(desc, entityType);
                    }

                    if (mustWrite)
                    {
                        propConverter = context.GetConverter(entityType);
                        propConverter.ToXml(entity, desc, value, writer, context);
                    }
                }
                else
                {
                    if (context.Settings.WriteNullValues)
                    {
                        if (entityDescriptor.CanWriteNullValue(desc.Metadata.PropertyName))
                        {
                            if (writeAttribute)
                            {
                                mustWrite = entityDescriptor.IsAttributeNullValueProperty(desc.Metadata.PropertyName);
                            }
                            else
                            {
                                mustWrite = !entityDescriptor.IsAttributeNullValueProperty(desc.Metadata.PropertyName);
                            }

                            if (mustWrite)
                            {
                                propConverter = context.GetConverter(desc.Metadata.PropertyType);
                                propConverter.ToXml(entity, desc, value, writer, context);
                            }
                        }
                    }
                }
            }
        }