title: c# Deserialize XML nested collections
body: <p>Hi I have some XML I wish to deserialise to .NET POCOs using the XMLSerializer</p>
  
  <p>the xml looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;message uuid="{2f1e274c-6a53-afea-3047-6dc739539656}"&gt;
      &lt;envelope received="a_date" subject="a_name"&gt;
        &lt;from&gt;
          &lt;monitor name="Local Folder" user_description="" uuid="{668DC658-97D7-42c8-AE72-ED289DD02355}"/&gt;
        &lt;/from&gt;
        &lt;to&gt;
          &lt;account&gt;
            &lt;factory name="a_name"/&gt;
          &lt;/account&gt;
        &lt;/to&gt;
      &lt;/envelope&gt;
      &lt;status&gt;
        &lt;action name="Folder" occured="a_date" type="monitor"&gt;
          &lt;session completed="a_date" name="a_name" started="a_date"/&gt;
        &lt;/action&gt;
        &lt;action occured="a_date" type="monitor"/&gt;
        &lt;action occured="a_date" type="translate"&gt;
          &lt;session completed="a_date" current="a_number" name="a_name" started="a_date" total="a_number" unit="time"/&gt;
          &lt;session completed="a_date" current="a_number" name="a_name" started="a_date" total="a_number" unit="time"/&gt;
        &lt;/action&gt;
        &lt;action occured="a_date" type="deliver"&gt;
          &lt;session completed="a_date" current="a_number" name="a_name" started="a_date" total="a_number" unit="byte"/&gt;
          &lt;session completed="a_date" name="a_name" started="a_date" unit="byte"/&gt;
          &lt;session completed="a_date" current="a_number" name="a_name" started="a_date" total="a_number" unit="byte"/&gt;
        &lt;/action&gt;
        &lt;action occured="a_date" type="complete"/&gt;
      &lt;/status&gt;
      &lt;host name="a_name"/&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>Within the xml, I have a status section which contains a collection of actions, each action may contain a collection of sessions.</p>
  
  <p>I have created classes for the XMLSerialiser to deserialize the xml:</p>
  
  <pre><code>namespace myNameSpace
  {
      [XmlRoot("message")]
      public class message
      {
          [XmlAttribute("uuid")] 
          public string uuid { get; set; }
          [XmlElement("envelope")]
          public envelope envelope { get; set; }
          [XmlArray("status")]
          [XmlArrayItem(typeof(action))]
          public ObservableCollection&lt;action&gt; status { get; set; }
          [XmlElement("host")]
          public host host { get; set; }
      }
  
      public class envelope
      {
          [XmlAttribute("received")] 
          public string received { get; set; }
          [XmlAttribute("subject")]
          public string subject { get; set; }
          [XmlElement("from")]
          public from from { get; set; }
          [XmlElement("to")]
          public to to { get; set; }
      }
  
      #region envelope element definitions
  
      public class from
      {
          [XmlElement("monitor")]
          public monitor monitor { get; set; }
  
      }
  
      public class monitor
      {
          [XmlAttribute("name")]
          public string name { get; set; }
          [XmlAttribute("user_description")]
          public string user_description { get; set; }
          [XmlAttribute("uuid")]
          public string uuid { get; set; }
  
      }
  
      public class to
      {
          [XmlElementAttribute("account")]
          public account account { get; set; }
      }
  
      public class account
      {
          [XmlElementAttribute("factory")]
          public factory factory { get; set; }
      }
  
      public class factory
      {
          [XmlAttribute("name")]
          public string name { get; set; }
      }
  
      #endregion
  
      public class action
      {
          [XmlAttribute("name")]
          public string name { get; set; }
          [XmlAttribute("occured")]
          public string occured { get; set; }
          [XmlAttribute("type")]
          public string type { get; set; }
          [XmlArray("action")]
          [XmlArrayItem(typeof(session))]
          public ObservableCollection&lt;session&gt; session { get; set; }
      }
  
      public class session
      {
          [XmlAttribute("completed")]
          public string completed { get; set; }
          [XmlAttribute("current")]
          public long current { get; set; }
          [XmlAttribute("name")]
          public string name { get; set; }
          [XmlAttribute("started")]
          public string started { get; set; }
          [XmlAttribute("total")]
          public long total { get; set; }
          [XmlAttribute("unit")]
          public string unit { get; set; }
      }
  
      public class host
      {
          [XmlAttribute("name")]
          public string name { get; set; }
      }
  }
  </code></pre>
  
  <p>Mostly I get the object graph I desire with all the values correctly deserialzed, but I can not find a way to get the XMLSerialiser to deserialize the session collection within an action element - The are always empty.</p>
  
  <p>Does anyone know how I might build my POCOs so that the XMLserialiser can create the session collections?</p>
  
  <p>best regards</p>
  
  <p>John.</p>