title: C# Deserialize of XML into entity
body: <p>I'm trying to deseaqralize this XML document into a list of Car objects, but it is coming up null.</p>
  
  <p>Here's a sample XML document based on this post:
    <a href="https://stackoverflow.com/questions/364253/how-to-deserialize-xml-document?rq=1">How to Deserialize XML document</a></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Cars&gt;
    &lt;car id="1"&gt;
      &lt;StockNumber&gt;1020&lt;/StockNumber&gt;
      &lt;Make&gt;Nissan&lt;/Make&gt;
      &lt;Model&gt;Sentra&lt;/Model&gt;
    &lt;/car&gt;
    &lt;car id="2"&gt;
      &lt;StockNumber&gt;1010&lt;/StockNumber&gt;
      &lt;Make&gt;Toyota&lt;/Make&gt;
      &lt;Model&gt;Corolla&lt;/Model&gt;
    &lt;/car&gt;
    &lt;car id="3"&gt;
      &lt;StockNumber&gt;1111&lt;/StockNumber&gt;
      &lt;Make&gt;Honda&lt;/Make&gt;
      &lt;Model&gt;Accord&lt;/Model&gt;
    &lt;/car&gt;
  &lt;/Cars&gt;
  </code></pre>
  
  <p>Required Classes:</p>
  
  <pre><code>[Serializable()]
  public class Car
  {
      [System.Xml.Serialization.XmlAttribute("id")]
      public int id { get; set; }
  
      [System.Xml.Serialization.XmlElement("StockNumber")]
      public string StockNumber { get; set; }
  
      [System.Xml.Serialization.XmlElement("Make")]
      public string Make { get; set; }
  
      [System.Xml.Serialization.XmlElement("Model")]
      public string Model { get; set; }
  }
  
  [Serializable()]
  [System.Xml.Serialization.XmlRoot("Cars")]
  public class Cars
  {
      [XmlArray("Cars")]
      [XmlArrayItem("Car", typeof(Car))]
      public List&lt;Car&gt; Car { get; set; }        
  }
  </code></pre>
  
  <p>Deserialize function:</p>
  
  <pre><code>public void ParseReturnXmlForVirtualEvent2()
  {
      Cars cars = null;
      string path = @"E:\Projects\Newcars.xml";
  
      XmlDocument pdoc = new XmlDocument();
      pdoc.Load(path);
      XDocument Doc = new XDocument();
      Doc = XDocument.Parse(pdoc.OuterXml);            
  
      System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Cars));
  
      System.Xml.XmlReader reader = Doc.CreateReader();
      cars = (Cars)serializer.Deserialize(reader);
      reader.Close();
  
      //return cars;
  }
  </code></pre>
  
  <p>Let me know if you need me to provide further details.</p>