title: How to set the name of an XML element on the class rather than the property
body: <p>I'm trying to do what seems like a simple task, but it's proving to be quite complicated.</p>
  
  <p>.NET's <code>System.Xml.Serialization</code> namespace seems to do a really good job of supporting static XML structure, but not so much dynamic structure.</p>
  
  <p>I'm trying to set up an XML document with elements whose children could have one of many types and in arbitrary order. It is my understanding that you can name lists of child elements with the same name this way:</p>
  
  <pre><code>[Serializable]
  [XmlRoot("Parent")]
  public class MyElement
  {
      [XmlElement("Child")]
      public string[] MyChildren { get; set; }
  }
  </code></pre>
  
  <p>This will result in XML that looks like this:</p>
  
  <pre><code>&lt;Parent&gt;
      &lt;Child&gt;&lt;/Child&gt;
      ...
      &lt;Child&gt;&lt;/Child&gt;
  &lt;/Parent&gt;
  </code></pre>
  
  <p>I'm trying to have a structure that looks like this:</p>
  
  <pre><code>&lt;Parent&gt;
      &lt;ChildTypeA&gt;&lt;/ChildTypeA&gt;
      &lt;ChildTypeB&gt;&lt;/ChildTypeB&gt;
      ...
      &lt;ChildTypeZ&gt;&lt;/ChildTypeZ&gt;
  &lt;/Parent&gt;
  </code></pre>
  
  <p>where there is no particular order to them, and types can appear more than once. I've seen some answers where people suggest to use the <code>XmlType</code> attribute on classes to declare the element name, but it seems that the functionality changed between then and now, because all that type does is declare the schema type of the element:</p>
  
  <pre><code>[Serializable]
  [XmlRoot("Parent")]
  public class MyElement
  {
      [XmlElement]
      public BaseChildElement[] MyChildren { get; set; }
  }
  
  [Serializable]
  public abstract class BaseChildElement {}
  
  [Serializable]
  [XmlType("ChildTypeA")]
  public class ChildElementA : BaseChildElement
  {
      [XmlAttribute]
      public string Content { get; set; }
  }
  
  [Serializable]
  [XmlType("ChildTypeB")]
  public class ChildElementB : BaseChildElement
  {
      [XmlAttribute]
      public string Content { get; set; }
  }
  </code></pre>
  
  <p>This will produce XML that looks like this:</p>
  
  <pre><code>&lt;Parent&gt;
      &lt;MyChildren xsi:type="ChildTypeA" Content="" /&gt;
      &lt;MyChildren xsi:type="ChildTypeB" Content="" /&gt;
      &lt;MyChildren xsi:type="ChildTypeA" Content="" /&gt;
      ...
  &lt;/Parent&gt;
  </code></pre>
  
  <p>Does someone know how to produce a dynamic list of child elements where the class of the child element gets to set the element name?</p>
  
  <p>HTML is a perfect example of what I'm trying to do. In HTML you can have child elements of arbitrary types in arbitrary order:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;
          &lt;p&gt;
              &lt;a href="google.com"&gt;Google&lt;/a&gt;
              &lt;span&gt;Some text&lt;/span&gt;
          &lt;p&gt;
          &lt;div&gt;
              &lt;button&gt;Click me&lt;/button&gt;
              &lt;a href="stackoverflow.com"&gt;Stack Overflow&lt;/a&gt;
          &lt;div&gt;
          &lt;p&gt;Hello world&lt;/p&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>