protected override void DoToXml(object parent, PropertyDescriptor metadata, object entity, XmlTextWriter writer, XmlSerializerContext context) { Type entityType = entity.GetType(); if (!TypeHelper.IsDefaultValue(entity, entityType) || context.Settings.WriteDefaultValues) { ValueTypePropertyDescriptor desc = metadata.GetPropertyDescriptor <ValueTypePropertyDescriptor>(entityType, context); string valueStr = this.GetValueAsString(desc, entity, entityType, context); if (!desc.IsXmlElement(entityType, context)) { string attributeName = metadata.GetAttributeNameForType(entityType, context); //esta definido el atributo, lo escribo writer.WriteAttributeString(attributeName, valueStr); } else { string elementName = desc.GetElementNameForType(entityType, context, true); //escribo el nombre de la propiedad writer.WriteStartElement(elementName); base.WriteTypeDefinition(desc, entityType, context, writer); if (desc.IsXmlAttribute(entityType, context)) { string attributeName = metadata.GetAttributeNameForType(entityType, context); //escribo el valor writer.WriteAttributeString(attributeName, valueStr); } else { writer.WriteRaw(valueStr); } //writer.WriteString(valueStr); si tiene caracteres de xml los convierte a & //cierro el tag writer.WriteEndElement(); } } }
protected override void DoToNullValueXml(object parent, PropertyDescriptor metadata, XmlTextWriter writer, XmlSerializerContext context) { Type entityType = metadata.Metadata.PropertyType; ValueTypePropertyDescriptor desc = metadata.GetPropertyDescriptor <ValueTypePropertyDescriptor>(entityType, context); if (!desc.IsXmlElement(entityType, context)) { string attributeName = metadata.GetAttributeNameForType(entityType, context); //esta definido el atributo, lo escribo writer.WriteAttributeString(attributeName, ""); } else { string elementName = desc.GetElementNameForType(entityType, context, true); //escribo el nombre de la propiedad writer.WriteElementString(elementName, ""); } }