title: XML Serialization of Derived Classes
body: <p>I have an array of elements that I need to serialize using XmlSerializer. The problem I'm having is I have 2 derived classes, and serializing them so they have an element name of the common base, doesn't seem to work. </p>
  
  <p>So, this is how the XML should look:</p>
  
  <pre><code>&lt;Root&gt;
      &lt;Base&gt; foo &lt;/Base&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>Instead, I'm getting</p>
  
  <pre><code>&lt;Root&gt;
      &lt;Derived1&gt; foo &lt;/Derived1&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>The code for the array of elements I am serializing is</p>
  
  <pre><code>private object[] m_nodes;
  
  [System.Xml.Serialization.XmlElementAttribute("Base", typeof(Derived1)]
  [System.Xml.Serialization.XmlElementAttribute("Base", typeof(Derived2)]
  
  public object[] Nodes
  {
      get
      {
          return this.m_nodes;
      }
      set
      {
          this.m_nodes = value;
      }
  }
  </code></pre>
  
  <p>With the above code, I get that there is a reflection error with Nodes. If I change "Base" in both XmlEelementAttributes to "Derived1" and Derived2", it works, but the element name is incorrect. </p>
  
  <pre><code>[System.Xml.Serialization.XmlInclude(typeof(Derived1))]
  public abstract class Base
  {
      public Base()
      {
  
      }
  }
  
  
  
  [System.SerializableAttribute()]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  public class Derived1: Base
  {
      public Derived1()
      {
  
      }
  }
  </code></pre>
  
  <p>Any help is greatly appreciated. Thanks so much. </p>