title: How to write XmlType for node names of classes that inherit from a base class using XmlSerializer
body: <p>I would like to generate the following XML from a group of C# objects:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;MyRoot xmlns="http://sample.com"&gt;
    &lt;Things&gt;
      &lt;This&gt;
        &lt;ThisRef&gt;this ref&lt;/ThisRef&gt;
      &lt;/This&gt;
      &lt;That&gt;
        &lt;ThatRef&gt;that ref&lt;/ThatRef&gt;
      &lt;/That&gt;
    &lt;/Things&gt;
  &lt;/MyRoot&gt;
  </code></pre>
  
  <p>Unfortunately, the XML Node <strong>Things</strong> can contain several <strong>This</strong> or <strong>That</strong> nodes, and I don't have any control over the XML Schema.  I need to create C# objects that will write the proper XML but I'm having trouble with the <strong>Things</strong> collection.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>[XmlRoot("MyRoot", Namespace = XmlNamespace)]
  public class MyRoot
  {
      public const string XmlNamespace = "http://sample.com";
      public List&lt;MyThing&gt; Things { get; set; }
  
  }
  [XmlInclude(typeof(This))]
  [XmlInclude(typeof(That))]
  public class MyThing
  {
  }
  [XmlRoot(Namespace = MyRoot.XmlNamespace)]
  [XmlType("This")]
  public class This : MyThing
  {
      public string ThisRef { get; set; }
  }
  [XmlRoot(Namespace = MyRoot.XmlNamespace)]
  [XmlType("That")]
  public class That : MyThing
  {
      public string ThatRef { get; set; }
  }
  [TestClass]
  public class so
  {
      [TestMethod]
      public void SerializeTest()
      {
          // Create object to serialize
          var data = new MyRoot { Things = new List&lt;MyThing&gt;() };
          data.Things.Add(new This { ThisRef = "this ref" });
          data.Things.Add(new That { ThatRef = "that ref" });
  
          // Create XML namespace
          var xmlNamespaces = new XmlSerializerNamespaces();
          xmlNamespaces.Add(string.Empty, MyRoot.XmlNamespace);
  
          // Write XML
          var serializer = new XmlSerializer(typeof(MyRoot));
          using (var writer = new StringWriter())
          {
              serializer.Serialize(writer, data, xmlNamespaces);
              var xml = writer.ToString();
              Console.WriteLine(xml);
          }
      }
  }
  </code></pre>
  
  <p>Generates the following XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;MyRoot xmlns="http://sample.com"&gt;
    &lt;Things&gt;
      &lt;MyThing d3p1:type="This" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"&gt;
        &lt;ThisRef&gt;this ref&lt;/ThisRef&gt;
      &lt;/MyThing&gt;
      &lt;MyThing d3p1:type="That" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance"&gt;
        &lt;ThatRef&gt;that ref&lt;/ThatRef&gt;
      &lt;/MyThing&gt;
    &lt;/Things&gt;
  &lt;/MyRoot&gt;
  </code></pre>
  
  <p>So, I have two questions:</p>
  
  <ol>
  <li>How can I get the XmlSerializer to write <strong>This</strong> and <strong>That</strong> nodes instead of <strong>MyThing</strong> nodes?</li>
  <li>How can I stop the XmlSerializer from adding additional namespace information to the <strong>This</strong> and <strong>That</strong> nodes (currently written as <strong>MyThing</strong> nodes)?</li>
  </ol>