title: c# - Deserialize XML w/ Nested Elements Using Attributes
body: <p>I need to deserialize an XML file. So far i have been successful using the <code>[XmlElement]</code> attribute. It works great. Where i am tripping up is when it comes to a nested element. For example <code>&lt;root&gt;&lt;level1&gt;&lt;set1&gt;&lt;data1&gt;x&lt;/data1&gt;&lt;data2&gt;y&lt;/data2&gt;&lt;/set1&gt;.....&lt;/root&gt;</code> and I only wish to capture data2 inside of set 1. I've experimented with [XmlArray] and [XmlArrayItem] but havent had much luck. I could be wrong but I didnt think a List would be appropriate since I only want one value for the class attribute. There is a bettere example bellow more specific to my problem. In it i would be concerned with capturing the <code>&lt;NAME&gt;</code> element inside of <code>&lt;H_DATA_1&gt;</code> that is nested in <code>&lt;HEADER&gt;</code> I have been successful in capturing what I need from the 1st <code>&lt;METADATA&gt;</code> inside of <code>&lt;PRODUCTION_SET&gt;</code> as well as pulling what i need from the 2nd <code>&lt;METADATA&gt;</code> inside of <code>&lt;PRODUCING_ENTITY&gt;</code> however my methods do not seem to be able to handle the additional nesting inside of the header tag. </p>
  
  <p><strong>XML</strong></p>
  
  <pre><code>&lt;PRODUCTION_SET&gt;
      &lt;METADATA&gt;
          &lt;M_DATA_1&gt;X&lt;/M_DATA_1&gt;
          &lt;M_DATA_2&gt;Y&lt;/M_DATA_2&gt;
          &lt;M_DATA_3&gt;Z&lt;/M_DATA_3&gt;
      &lt;/METADATA&gt;
      &lt;PRODUCING_ENTITY&gt;
          &lt;METADATA&gt;
              &lt;PM_Data_1&gt;X&lt;PM_Data_1&gt;
              &lt;PM_Data_2&gt;Y&lt;PM_Data_2&gt;
          &lt;/Metadata&gt;
          &lt;HEADER&gt;
              &lt;H_DATA_1&gt;
                  &lt;NAME&gt;I NEED THIS NAME&lt;/NAME&gt;
              &lt;/H_DATA_1&gt;
              &lt;H_DATA_2&gt;
                  &lt;NAME&gt;I ALSO NEED THIS NAME&lt;/NAME&gt;
                  &lt;CODE&gt;DO NOT WANT THIS CODE&lt;/CODE&gt;
              &lt;/H_DATA_2&gt;
              &lt;H_DATA_3&gt;
                  &lt;NAME&gt;I DO NOT NEED THIS NAME&lt;/NAME&gt;
              &lt;/H_DATA_3&gt;
          &lt;/HEADER&gt;
      &lt;/PRODUCING_ENTITY&gt;
      &lt;PRODUCING_ENTITY&gt;
      .
      .
      .
      &lt;/PRODUCING_ENTITY&gt;
  .
  .
  .
  .
  &lt;/PRODUCING_SET&gt;
  </code></pre>
  
  <p><strong>CLASS</strong></p>
  
  <pre><code>[Serializable()]
  [XmlRoot("PRODUCTION_SET")]
  public class ProductionSet
  {
      [XmlElement("METADATA")]
      public List&lt;Metadata&gt; Metadata { get; set; }
  
      [XmlElement("PRODUCING_ENTITY")]
      public List&lt;Producing_Entity&gt; ProducingEntity { get; set; }
  
  }
  
  public class Metadata
  {
      [XmlElement("M_DATA_1")]
      public string mData1 { get; set; }
      [XmlElement("M_DATA_2")]
      public string mdata2 { get; set; }
  }
  
  public class Producing_Entity
  {
      [XmlElement("METADATA")]
      public List&lt;ProdMeta&gt; ProdMeta { get; set; }
  
      [XmlElement("HEADER")]
      public List&lt;Header&gt; Header { get; set; }
  
  
  }
  
  public class ProdMeta
  {
      [XmlElement("PM_DATA_1")]
      public string pmData1{ get; set; }
  
  }
  
  
  public class Header
  {
      [XmlElement("H_DATA1")]
      [XmlElement("NAME")]
      public string H_DATA1_Name { get; set; }
  
  
      [XmlElement("H_DATA2")]
      [XmlElement("NAME")]
      public string H_DATA2_Name { get; set; }
  
  
  }
  </code></pre>