Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Really tricky ... <a href="https://stackoverflow.com/questions/6051228/ixmlserializable-and-inheritance">XmlSerialization is not polymorphic</a> so your XML usually provides a flattened version of your object hierarchie. I see two possible implementations. The first exposes the base property as another name (see "ConcreteChild"). The other implements IXmlSerialization (see "ConcreteChild2") so that you can write your custom nesting of properties.</p> <pre><code>// Base Class [Serializable] [XmlInclude(typeof(ConcreteChild))] public class BaseChild { public BaseChild() { ChildName = "Base"; } public String ChildName { get; set; } } // Exposing Parent Property [Serializable] public class ConcreteChild : BaseChild { public new String ChildName { get; set; } public String BaseChildName { get { return ((BaseChild) this).ChildName; } set { ((BaseChild)this).ChildName = value; } } } // Writing Custung Serializable [Serializable] public class ConcreteChild2 : BaseChild, IXmlSerializable { public new String ChildName { get; set; } public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { } public void WriteXml(XmlWriter writer) { writer.WriteStartElement("InherittedChild"); writer.WriteElementString("ConcreteChildName", ChildName); writer.WriteEndElement(); // Since BaseChild does not implement IXmlSerializable // we cannot use base.WriteXml(writer); writer.WriteElementString("BaseChildName", ((BaseChild) this).ChildName); } } [XmlInclude(typeof(ConcreteChild))] [XmlInclude(typeof(ConcreteChild2))] [Serializable] public class MyClass { public BaseChild Left { get; set; } [XmlElement("ConcreteChild2", typeof(ConcreteChild2))] // does not work without !!! public BaseChild Right { get; set; } } </code></pre> <p><strong>Result:</strong> You can see both serializations in the MyClass structure</p> <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt; &lt;MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Left xsi:type="ConcreteChild"&gt; &lt;ChildName&gt;Left&lt;/ChildName&gt; &lt;BaseChildName&gt;Base&lt;/BaseChildName&gt; &lt;/Left&gt; &lt;ConcreteChild2&gt; &lt;InherittedChild&gt; &lt;ConcreteChildName&gt;Right&lt;/ConcreteChildName&gt; &lt;/InherittedChild&gt; &lt;BaseChildName&gt;Base&lt;/BaseChildName&gt; &lt;/ConcreteChild2&gt; &lt;/MyClass&gt; </code></pre> <p><strong>Serialization</strong></p> <pre><code>var mc = new MyClass(); mc.Left = new ConcreteChild { ChildName = "Left"}; mc.Right = new ConcreteChild2 { ChildName = "Right" }; StringWriter textWriter = new StringWriter(); XmlSerializer xmlSerializer = new XmlSerializer(mc.GetType()); xmlSerializer.Serialize(textWriter, mc); var s = textWriter.ToString(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload