title: How to Deserialize XMLElement with same name
body: <p>I have XML root element and XML Element with same name, I am not sure how I should change my model class</p>
  
  <p>The following code works as far as XML Element is not repeated with same name, in my case Gender list=1</p>
  
  <p>Changing XML out format is not possible as it is coming from another system, unless filter out in C# code level</p>
  
  <h2>XML</h2>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  
   &lt;Gender&gt;
     &lt;Gender list="1"&gt;
      &lt;Item&gt;
        &lt;CODE&gt;M&lt;/CODE&gt;
        &lt;DESCRIPTION&gt;Male&lt;/DESCRIPTION&gt; 
      &lt;/Item&gt;
      &lt;Item&gt;
        &lt;CODE&gt;F&lt;/CODE&gt;
        &lt;DESCRIPTION&gt;Female&lt;/DESCRIPTION&gt;
      &lt;/Item&gt;
     &lt;/Gender&gt;
    &lt;/Gender&gt;
  </code></pre>
  
  <h2>Model Class</h2>
  
  <pre><code> public class Gender
  {
  
      [XmlElement("Item")]
      public List&lt;Item&gt; GenderList = new List&lt;Item&gt;();
  }
  
  
  public class Item
  {
      [XmlElement("CODE")]
      public string Code { get; set; }
  
      [XmlElement("DESCRIPTION")]
      public string Description { get; set; }
  }
  </code></pre>
  
  <h2>XML Parsing Class</h2>
  
  <pre><code>public static class XMLPrasing
  {
      public static Object ObjectToXML(string xml, Type objectType)
      {
          StringReader strReader = null;
          XmlSerializer serializer = null;
          XmlTextReader xmlReader = null;
          Object obj = null;
          try
          {
              strReader = new StringReader(xml);
              serializer = new XmlSerializer(objectType);
              xmlReader = new XmlTextReader(strReader);
              obj = serializer.Deserialize(xmlReader);
          }
          catch (Exception exp)
          {
              //Handle Exception Code
              var s = "d";
          }
          finally
          {
              if (xmlReader != null)
              {
                  xmlReader.Close();
              }
              if (strReader != null)
              {
                  strReader.Close();
              }
          }
          return obj;
      }
  </code></pre>
  
  <h2>SECOND UPDATE</h2>
  
  <p>If I change my code with different Gender name as following then this work, question remain same how to handle with same name</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  
  &lt;Gender&gt;
   &lt;GenderX list="1"&gt;
     &lt;Item&gt;
      &lt;CODE&gt;M&lt;/CODE&gt;
      &lt;DESCRIPTION&gt;Male&lt;/DESCRIPTION&gt;
     &lt;/Item&gt;
     &lt;Item&gt;
      &lt;CODE&gt;F&lt;/CODE&gt;
      &lt;DESCRIPTION&gt;Female&lt;/DESCRIPTION&gt;
     &lt;/Item&gt;
    &lt;/GenderX&gt;
   &lt;/Gender&gt;
  </code></pre>
  
  <h2>Model class</h2>
  
  <pre><code>  [XmlRoot("Gender")]
  public class Gender
  {
  
      [XmlElement("GenderX")]
      public List&lt;GenderX&gt; GenderXList = new List&lt;GenderX&gt;();       
  }
  
  
  public class GenderX
  {
      [XmlElement("Item")]
      public List&lt;Item&gt; GenderList = new List&lt;Item&gt;();
  }
  
  
  
  public class Item
  {
      [XmlElement("CODE")]
      public string Code { get; set; }
  
      [XmlElement("DESCRIPTION")]
      public string Description { get; set; }
  }
  </code></pre>