title: How to serialize / deserialize xml into a C# object?
body: <p>How can I serialize / deserialize this xml string into a C# object?</p>
  
  <pre><code>&lt;Response&gt;
      &lt;Business Number="99696" Name="My business" Address=""  /&gt;
      &lt;Purchase PurchaseID="7" CustomerID="0" &gt;
          &lt;Item Name="item 1" Qty="100" UnitCost="10.0000" /&gt;
          &lt;Item Name="item2" Qty="200" UnitCost="20.0000" /&gt;
      &lt;/Purchase&gt;
  &lt;/Response&gt;
  </code></pre>
  
  <p>Thank you
  rubenc</p>
  
  <p>I think is a different question because all the questions normally talk about one level and or a list of items</p>
  
  <p>My problem is that I have different levels:</p>
  
  <pre><code>&lt;Response&gt;               //root
      &lt;Business ... /&gt;     //level 1
      &lt;Purchase... &gt;       //level 1
          &lt;Item ... /&gt;     // list
  </code></pre>
  
  <p>I get null in number, name etc.</p>
  
  <p>This is what I have tried so far:</p>
  
  <pre><code>    [Serializable, XmlRoot("Response")]
      public class Response
      {
          public Depot depot = new Depot();
  
          // I have tried this also:
          //[XmlElement("Number")]
          //public string Number { get; set; }
  
          //[XmlElement("Name")]
          //public string Name { get; set; }
      }
  
      public class Depot
      {
          [XmlElement("Number")]
          public string Number { get; set; }
  
          [XmlElement("Name")]
          public string Name { get; set; }
      }
  
  
      static object DeserializeResponse(string responseString)
      {
          var serializer = new XmlSerializer(typeof(Response));
  
          Response result;
          using (var reader = new StringReader(responseString))
          {
              result = (Response)serializer.Deserialize(reader);
          }
  
          return result;
      }
  </code></pre>