title: Accessing Elements from multi-level (nested) XML documents in c#
body: <p>I am pulling XML data from a server using an API and use LINQ to store the results in an anonymous object list, however some of the data is returning "null" and I figured it must be due to the XML document heirarchy.  See below-</p>
  
  <pre><code>&lt;export type="orderexport" date="13/01/2015" time="5:01:18 PM"&gt;
    &lt;order&gt;
      &lt;OrderNumber&gt;149612&lt;/OrderNumber&gt;
      &lt;CustomerNumber&gt;146538&lt;/CustomerNumber&gt;
      &lt;Currency&gt;AUD&lt;/Currency&gt;
      &lt;Locale&gt;en_AU&lt;/Locale&gt;
      &lt;GrandTotal&gt;1548.98&lt;/GrandTotal&gt;
      &lt;TotalBeforeTax&gt;1411.58&lt;/TotalBeforeTax&gt;
      &lt;TotalTax&gt;137.4&lt;/TotalTax&gt;
      &lt;Addresses&gt;
        &lt;BillingAddress&gt;
          &lt;Email&gt;XXXXXX@XXXXXXX.com.au&lt;/Email&gt;
        &lt;/BillingAddress&gt;
        &lt;ShippingAddress&gt;
          &lt;Email&gt;XXXXXX@XXXXXXX.com.au&lt;/Email&gt;
        &lt;/ShippingAddress&gt;
      &lt;/Addresses&gt;
      &lt;LineItems&gt;
        &lt;LineItem&gt;...&lt;/LineItem&gt;
        &lt;LineItemSalesDiscount&gt;...&lt;/LineItemSalesDiscount&gt;
        &lt;LineItemDiscount&gt;...&lt;/LineItemDiscount&gt;
        &lt;LineItemShipping&gt;...&lt;/LineItemShipping&gt;
        &lt;LineItemPayment&gt;...&lt;/LineItemPayment&gt;
        &lt;LineItemPaymentDiscount&gt;...&lt;/LineItemPaymentDiscount&gt;
      &lt;/LineItems&gt;
      &lt;CreationDate&gt;13/01/2015 9:42:59 AM&lt;/CreationDate&gt;
      &lt;ViewedOn&gt;13/01/2015 9:44:20 AM&lt;/ViewedOn&gt;
      &lt;cancelledon/&gt;
      &lt;readyforshippingon&gt;13/01/2015 1:58:41 PM&lt;/readyforshippingon&gt;
      &lt;paidon&gt;13/01/2015 9:43:15 AM&lt;/paidon&gt;
      &lt;ordercomment&gt;
        removed
      &lt;/ordercomment&gt;
    &lt;/order&gt;
  &lt;/export&gt;
  </code></pre>
  
  <p>I can pull all the elements fine at the first level, however I need to include the  tag but it is nested under another level, when I try to get its value like I get the rest of it I get a null value.</p>
  
  <pre><code>        //Create Request
          myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
          myHttpWebRequest.Method = "GET";
          myHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";
  
          //Get Response
          myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
  
          //Load response stream into XMLReader
          myXMLReader = new XmlTextReader(myHttpWebResponse.GetResponseStream());
  
          var xdoc = XDocument.Load(myXMLReader);
  
          var result = from x in xdoc.Root.Elements()
                       select new
                       {
                           OrderNumber = (string)x.Element("OrderNumber"),
                           CustomerNumber = (string)x.Element("CustomerNumber"),
                           GrandTotal = (double)x.Element("GrandTotal"),
                           Email = (string)x.Element("Email")
                       };
  </code></pre>
  
  <p><strong>OUTPUT:</strong> "{ OrderNumber = "149612", CustomerNumber = "146538", GrandTotal = 1548.98, Email = null }"</p>
  
  <p>I have never read an XML document before but I'm fairly sure the above code is right, I'm sure I just need to change the LINQ expression line for Email but I don't know what it should be.</p>
  
  <p>Regards</p>