title: Deserialize dynamic XML
body: <p>The <code>XML</code> below always comes in this format, but the elements under the <code>&lt;Hit&gt;</code> node are dynamic, the name or number of items could be different each time. Is it possible to get the elements under the <code>&lt;Hit&gt;</code> node dynamically.</p>
  
  <pre><code>&lt;SearchResponse&gt;
      &lt;Response&gt;
          &lt;Qtime&gt;3&lt;/Qtime&gt;
          &lt;HitsPerPage&gt;10&lt;/HitsPerPage&gt;
      &lt;/Response&gt;
      &lt;HitsCount&gt;
          &lt;total&gt;33&lt;/total&gt;
          &lt;start&gt;0&lt;/start&gt;
          &lt;end&gt;10&lt;/end&gt;
      &lt;/HitsCount&gt;
      &lt;Hits&gt;
          &lt;Hit&gt;
              &lt;id type=''&gt;123&lt;/id&gt;
              &lt;eid type=''&gt;456&lt;/eid&gt;
              &lt;Title type='t'&gt;
                  &lt;![CDATA[title goes here]]&gt;
              &lt;/Title&gt;
          &lt;/Hit&gt;
          &lt;Hit&gt;
              &lt;id type=''&gt;123&lt;/id&gt;
              &lt;oid type=''&gt;456&lt;/oid&gt;
              &lt;Title type='t'&gt;
                  &lt;![CDATA[title goes here]]&gt;
              &lt;/Title&gt;
              &lt;Description type='s'&gt;
                  &lt;![CDATA[Description goes here]]&gt;
              &lt;/Description&gt;
          &lt;/Hit&gt;
      &lt;/Hits&gt;
  &lt;/SearchResponse&gt;
  </code></pre>
  
  <p>Edit: here's the C# code, it's working fine and get the id from the <code>&lt;Hit&gt;</code> node since I already defined the property, but I need to get all of them dynamic.</p>
  
  <pre><code>[XmlRoot("SearchResponse")]
      public sealed class SearchResponse {
          [XmlElement("Response", Type = typeof(Response))]
          public Response[] Responses { get; set; }
  
          [XmlElement("HitsCount", Type = typeof(HitsCount))]
          public HitsCount[] HitsCount { get; set; }
  
          [XmlElement("Hits", Type = typeof(Hits))]
          public Hits[] Hits { get; set; }
  
  
          public static SearchResponse GetSearchResponseObject(string xmlString) {
              try {
                  var reader = new StringReader(xmlString);
                  var serializer = new XmlSerializer(typeof(SearchResponse));
                  var instance = (SearchResponse)serializer.Deserialize(reader);
  
                  return instance;
              } catch (Exception ex) {
                  var asd = ex.Message;
                  return null;
              }
          }
      }
  
      [Serializable]
      public class Response {
          [XmlElement("Qtime")]
          public string Qtime { get; set; }
  
          [XmlElement("HitsPerPage")]
          public string HitsPerPage { get; set; }
  
      }
  
      [Serializable]
      public class HitsCount {
          [XmlElement("total")]
          public string Total { get; set; }
  
          [XmlElement("start")]
          public string Start { get; set; }
  
          [XmlElement("end")]
          public string End { get; set; }
      }
  
      [Serializable]
      public class Hits {
  
          [XmlElement("Hit")]
          public Hit[] Hit { get; set; }
      }
  
      [Serializable]
      public class Hit {
          [XmlElement("id")]
          public string Id { get; set; }
      }
  </code></pre>
  
  <p>Edit 2: // Hit class code</p>
  
  <pre><code>public class Hit {
  
          // Since "id" is expected in the XML, deserialize it explicitly.
          [XmlElement("id")]
          public string Id { get; set; }
  
          private readonly List&lt;XElement&gt; _elements = new List&lt;XElement&gt;();
  
          [XmlAnyElement]
          public List&lt;XElement&gt; Elements { get { return _elements; } }
  
      }
  </code></pre>