public void Serialize(XmlTextWriter writer, object obj, bool writeElement) { IList list = (IList)obj; //Serialize the object and its public members //Generic types have a ` in the name so we have to strip it out if (writeElement) { writer.WriteStartElement(obj.GetType().Name.Replace("`", "")); } if (list.Count > 0) { //determine the converter for the type of the list if it is a generic //if it is not a generic then it will use a converter per type Type listType = GetGenericListType(list.GetType()); TypeConverter listTypeConverter = null; if (listType != null) { listTypeConverter = TypeDescriptor.GetConverter(listType); } if (!DefaultObjectSerializer.CanSerialize(listTypeConverter)) { listTypeConverter = null; } foreach (object listEntry in list) { //If we have a item element name specified then use it otherwise use the name of the type string elementName = (itemElementName != null) ? itemElementName : listEntry.GetType().Name; if (listTypeConverter != null) { writer.WriteElementString(elementName, listTypeConverter.ConvertToString(null, DefaultObjectSerializer.InvariantSerializationCulture, listEntry)); } else { writer.WriteStartElement(elementName); ObjectSerializer.Serialize(writer, listEntry, false); writer.WriteEndElement(); } } } if (writeElement) { writer.WriteEndElement(); //Object element } }
public void Serialize(XmlTextWriter writer, object obj, bool writeElement) { //Use the IXmlSerializable implementation if needed IXmlSerializable serializableObject = obj as IXmlSerializable; if (serializableObject != null) { serializableObject.WriteXml(writer); return; } //If the type can be converted directly to a string then serialize it //as an element with text content (int, string, etc) TypeConverter conv = TypeDescriptor.GetConverter(obj); if (CanSerialize(conv)) { writer.WriteElementString(obj.GetType().Name.ToLowerInvariant(), conv.ConvertToInvariantString(obj)); return; } //Serialize the object and its public members if (writeElement) { writer.WriteStartElement(obj.GetType().Name); } //Get the list of properties with the ones that want to be serialized as attributed listed first List <PropertyDescriptor> props = GetSortedProperties(obj); //Serialize the Properties foreach (PropertyDescriptor prop in props) { //Get the property value object value = prop.GetValue(obj); if (value == null) { continue; } if (prop.IsReadOnly) { //if the property is read-only and is a list then serialize the content IList list = value as IList; if (list != null && list.Count > 0) { writer.WriteStartElement(prop.Name); string elementName = prop.Name.Substring(0, prop.Name.Length - 1); DefaultListSerializer serializer = new DefaultListSerializer(elementName); serializer.Serialize(writer, list, false); writer.WriteEndElement(); } } else { //Get a converter and Serialize it to a string TypeConverter converter = prop.Converter; if (converter == null || !converter.CanConvertTo(typeof(string)) || !converter.CanConvertFrom(typeof(string))) { writer.WriteStartElement(prop.Name); ObjectSerializer.Serialize(writer, value, false); writer.WriteEndElement(); continue; } string strValue = converter.ConvertToString(null, InvariantSerializationCulture, value); if (prop.Attributes.Contains(new XmlAttributeAttribute())) { writer.WriteAttributeString(prop.Name, strValue); } else { writer.WriteElementString(prop.Name, strValue); } } } if (writeElement) { writer.WriteEndElement(); //Object element } }