title: Linq to XML Element inside Element
body: <p>I wrote a method to read XML and write information to an object. The XML contains elements with the information, but some of the information is encapsulated and I can't figure out how to get to the information out of it. The XML contains about 200 "results".</p>
  
  <p><strong>XML structure</strong></p>
  
  <pre><code> &lt;result id="xxxxx"&gt;
        &lt;name&gt;Name&lt;/name&gt;
        &lt;age&gt;25&lt;/age&gt;
        &lt;info&gt;
             &lt;x&gt;Some text&lt;/x&gt;
             &lt;y&gt;More Text&lt;/y&gt;
        &lt;/info&gt;
   &lt;/result&gt;
  </code></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>XDocument rootDocument = XDocument.Load(file);
  var xy = from r in rootDocument.Descendants("result")
          select new
          {
              Name = r.Element("name")
              Age = r.Element("age"),
              x = r.Element("info").Element("x"),
              y = r.Element("info").Element("y"),
          };
  
  foreach (var r in xy)
  {
      Object o = new Object()
      {
          Name = r.Name,
          Age = r.Age,
          x = r.x,
          y = r.y
      };
  }
  </code></pre>
  
  <p><strong>Error</strong>
  Object reference not set to an instance of an object.</p>
  
  <p>The Error occurs at the line</p>
  
  <pre><code>  x = r.Element("info")...
  </code></pre>
  
  <p>and the following one.</p>