title: Serializing Lists of Classes to XML
body: <p>I have a collection of classes that I want to serialize out to an XML file. It looks something like this:</p>
  
  <pre><code>public class Foo
  {
    public List&lt;Bar&gt; BarList { get; set; }
  }
  </code></pre>
  
  <p>Where a bar is just a wrapper for a collection of properties, like this:</p>
  
  <pre><code>public class Bar
  {
    public string Property1 { get; set; }
    public string Property2 { get; set; }
  }
  </code></pre>
  
  <p>I want to mark this up so that it outputs to an XML file - this will be used for both persistence, and also to render the settings via an XSLT to a nice human-readable form. </p>
  
  <p>I want to get a nice XML representation like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Foo&gt;
    &lt;BarList&gt;
      &lt;Bar&gt;
        &lt;Property1&gt;Value&lt;/Property1&gt;
        &lt;Property2&gt;Value&lt;/Property2&gt;   
      &lt;/Bar&gt;
      &lt;Bar&gt;
        &lt;Property1&gt;Value&lt;/Property1&gt;
        &lt;Property2&gt;Value&lt;/Property2&gt;   
      &lt;/Bar&gt;
    &lt;/Barlist&gt;
  &lt;/Foo&gt;
  </code></pre>
  
  <p>where are all of the Bars in the Barlist are written out with all of their properties. I'm fairly sure that I'll need some markup on the class definition to make it work, but I can't seem to find the right combination.</p>
  
  <p>I've marked Foo with the attribute  </p>
  
  <pre><code>[XmlRoot("Foo")]  
  </code></pre>
  
  <p>and the <code>list&lt;Bar&gt;</code> with the attribute</p>
  
  <pre><code>[XmlArray("BarList"), XmlArrayItem(typeof(Bar), ElementName="Bar")]
  </code></pre>
  
  <p>in an attempt to tell the Serializer what I want to happen. This doesn't seem to work however and I just get an empty tag, looking like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Foo&gt; 
    &lt;Barlist /&gt;
  &lt;/Foo&gt;
  </code></pre>
  
  <p>I'm not sure if the fact I'm using Automatic Properties should have any effect, or if the use of generics requires any special treatment. I've gotten this to work with simpler types like a list of strings, but a list of classes so far eludes me.</p>