IXmlSerializable little tedious to implement, since it is almost completely or nothing works, provided that you cannot select child types for regular XML serialization. However, if you understand correctly, you can achieve what you want by manually creating an XmlSerializer for types that do not implement IXmlSerializable .
For example, if we start with two classes, Default , which does not implement IXmlSerializable and Custom , which implement it.
public class Default // Uses default XML Serialization { public int Count { get; set; } } public class Custom : IXmlSerializable { public int Count { get; set; } public XmlSchema GetSchema() { throw new NotImplementedException(); } public void ReadXml(XmlReader reader) { reader.ReadToDescendant("Count"); this.Count = reader.ReadElementContentAsInt(); } public void WriteXml(XmlWriter writer) { writer.WriteStartElement("Custom"); writer.WriteElementString("Count", this.Count.ToString()); writer.WriteEndElement(); } }
Then we create a third Parent class that has a child of each of the previous instances, and implements IXmlSerializable in a way that calls the ReadXml/WriteXml on the child that supports it, and creates a default default XML serializer for another child.
public class Parent : IXmlSerializable { public Parent() { this.Default = new Default { Count = 1 }; this.Custom = new Custom { Count = 2 }; } public Default Default { get; set; } public Custom Custom { get; set; } public XmlSchema GetSchema() { throw new NotImplementedException(); } public void ReadXml(XmlReader reader) { reader.ReadToFollowing("Custom"); this.Custom = new Custom(); this.Custom.ReadXml(reader); reader.ReadToFollowing("Default"); var serializer = new XmlSerializer(typeof(Default)); this.Default = (Default)serializer.Deserialize(reader); } public void WriteXml(XmlWriter writer) { this.Custom.WriteXml(writer); var ns = new XmlSerializerNamespaces(); ns.Add("", ""); new XmlSerializer(typeof(Default)).Serialize(writer, this.Default, ns); } }
To make the example complete, a sample program that serializes and deserializes the Parent instance:
static void Main() { var sb = new StringBuilder(); var serializer = new XmlSerializer(typeof(Parent)); serializer.Serialize(new StringWriter(sb), new Parent()); Console.WriteLine(sb); var parent = (Parent)serializer.Deserialize(new StringReader(sb.ToString())); Console.WriteLine("Parent.Custom.Count: {0}", parent.Custom.Count); Console.WriteLine("Parent.Default.Count: {0}", parent.Default.Count); }
João Angelo
source share