title: C# Parse XML into Object with child elements
body: <p>I am attempting to read an XML file into a C# object. The XML contains multiple child elements. Using the below code I can access all the top fields (version, live, pageid, etc.) with no problems, however when I try to access a child node value, I receive a </p>
  
  <pre><code>Object reference not set to an instance of an object.
  </code></pre>
  
  <p>which I assume is indicating that XMLSerializer can't match the nodes with my object. I have tried different object types such as a List field over an array but still seem to get the same results, so I'm unsure what the best way to approach this is?</p>
  
  <p>Any help pointing my in the right direction would be much appreciated.</p>
  
  <p><strong>XML</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
    &lt;LiveModelStruct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
     &lt;version&gt;1&lt;/version&gt;
     &lt;live&gt;true&lt;/live&gt;
     &lt;pageid&gt;1&lt;/pageid&gt;
     &lt;data&gt;test data&lt;/data&gt;
     &lt;giveawayactive&gt;false&lt;/giveawayactive&gt;
     &lt;giveawayserial&gt;00000000&lt;/giveawayserial&gt;
     &lt;templates&gt;
       &lt;template&gt;
         &lt;id&gt;1&lt;/id&gt;
         &lt;title&gt;Template 1&lt;/title&gt;
         &lt;type&gt;Opener&lt;/type&gt;
       &lt;/template&gt;
       &lt;template&gt;
         &lt;id&gt;2&lt;/id&gt;
         &lt;title&gt;Template 2&lt;/title&gt;
         &lt;type&gt;Song&lt;/type&gt;
       &lt;/template&gt;
    &lt;/templates&gt;
   &lt;/LiveModelStruct&gt;
  </code></pre>
  
  <p><strong>Object</strong></p>
  
  <pre><code>[XmlRoot(ElementName = "LiveModelStruct")]
  public class LiveModelStruct
  {
      [XmlElement(ElementName = "version")]
      public string version { get; set; }
      [XmlElement(ElementName = "live")]
      public string live { get; set; }
      [XmlElement(ElementName = "pageid")]
      public string pageid { get; set; }
      [XmlElement(ElementName = "data")]
      public string data { get; set; }
      [XmlElement(ElementName = "giveawayactive")]
      public string giveawayactive { get; set; }
      [XmlElement(ElementName = "giveawayserial")]
      public string giveawayserial { get; set; }
      [XmlElement(ElementName = "templates")]
      public Templates Templates { get; set; }
      [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
      public string Xsi { get; set; }
      [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
      public string Xsd { get; set; } 
  }
  
  [XmlRoot(ElementName = "templates")]
  public class Templates
  {
      [XmlElement(ElementName = "template")]
      public Template[] Template { get; set; }    
  }
  
  [XmlRoot(ElementName = "template")]
  public class Template
  {
      [XmlElement(ElementName = "id")]
      public string id { get; set; }
      [XmlElement(ElementName = "title")]
      public string title { get; set; }
      [XmlElement(ElementName = "type")]
      public string type { get; set; }
  }
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>...
  var serializer = new XmlSerializer(typeof(LiveModelStruct));
  var data = (LiveModelStruct)serializer.Deserialize(stream);
  var test1 = data.version;
  Debug.WriteLine(test1); //Returns 1 as it should
  
  var test = data.Templates.Template[0].title; //Throws Error
  Debug.WriteLine(test);
  </code></pre>