title: LINQ to XML - Reading XML Document
body: <p>Could someone please help me how to read the XML Document below using Linq to XML please?</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;cXML&gt;
  &lt;Request&gt;
  &lt;OrderRequest&gt;
    &lt;OrderRequestHeader orderID="xy1234" orderDate="2007-01-1T15:5400+10:00" type="new" orderVersion="001"&gt;
      &lt;Total&gt;
        &lt;Money currency="NZ"&gt;34.06&lt;/Money&gt;
      &lt;/Total&gt;
      &lt;ShipTo&gt;
        &lt;Address&gt;
          &lt;Name xml:lang="en"&gt;xyz&lt;/Name&gt;
          &lt;PostalAddress name="xyz"&gt;
            &lt;Street&gt;xyz street&lt;/Street&gt;
            &lt;City&gt;xyz&lt;/City&gt;              
          &lt;/PostalAddress&gt;
        &lt;/Address&gt;
      &lt;/ShipTo&gt;
      &lt;BillTo&gt;
        &lt;Address&gt;
          &lt;Name xml:lang="en"&gt;XYZ&lt;/Name&gt;
          &lt;PostalAddress name="XYZ"&gt;
            &lt;Street&gt;PO BOX 1234&lt;/Street&gt;
            &lt;City&gt;xyz&lt;/City&gt;
            &lt;State&gt;xyz state&lt;/State&gt;              
          &lt;/PostalAddress&gt;
        &lt;/Address&gt;
      &lt;/BillTo&gt;
      &lt;Contact role="xxx" addressID="123456789"&gt;
        &lt;Name xml:lang="en"&gt;XYZ&lt;/Name&gt;
        &lt;Email name="business"&gt;XYZ@ms.com.nz&lt;/Email&gt;
        &lt;Phone&gt;
          &lt;TelephoneNumber&gt;
            &lt;Number&gt;1234-1234&lt;/Number&gt;
          &lt;/TelephoneNumber&gt;
        &lt;/Phone&gt;
      &lt;/Contact&gt;
      &lt;Contact role="ShipTo"&gt;
        &lt;Name xml:lang="en"&gt;XYZ&lt;/Name&gt;
      &lt;/Contact&gt;
      &lt;Contact role="Supplier"&gt;
        &lt;Name xml:lang="en"&gt;XYZ pty ltd&lt;/Name&gt;
      &lt;/Contact&gt;
    &lt;/OrderRequestHeader&gt;
    &lt;ItemOut quantity="20" lineNumber="1" requestedDeliveryDate="2007-01-01T00:0000+10:00"&gt;
      &lt;ItemID&gt;
        &lt;SupplierPartID&gt;12345678&lt;/SupplierPartID&gt;
      &lt;/ItemID&gt;
      &lt;ItemDetail&gt;
        &lt;UnitPrice&gt;
          &lt;Money currency="NZ"&gt;32&lt;/Money&gt;
        &lt;/UnitPrice&gt;
        &lt;Description xml:lang="en"&gt;abc description&lt;/Description&gt;
        &lt;UnitOfMeasure&gt;CU&lt;/UnitOfMeasure&gt;
        &lt;Classification domain="N/A"/&gt;
        &lt;ManufacturerPartID&gt;12345678&lt;/ManufacturerPartID&gt;
        &lt;Extrinsic name="StockCode"&gt;12345&lt;/Extrinsic&gt;
        &lt;Extrinsic name="Quantity"&gt;1&lt;/Extrinsic&gt;          
      &lt;/ItemDetail&gt;
    &lt;/ItemOut&gt;
    &lt;ItemOut quantity="10" lineNumber="2" requestedDeliveryDate="2007-01-01T00:0000+10:00"&gt;
      &lt;ItemID&gt;
        &lt;SupplierPartID&gt;12345678&lt;/SupplierPartID&gt;
      &lt;/ItemID&gt;
      &lt;ItemDetail&gt;
        &lt;UnitPrice&gt;
          &lt;Money currency="NZ"&gt;32&lt;/Money&gt;
        &lt;/UnitPrice&gt;
        &lt;Description xml:lang="en"&gt;abc description&lt;/Description&gt;
        &lt;UnitOfMeasure&gt;CU&lt;/UnitOfMeasure&gt;
        &lt;Classification domain="N/A"/&gt;
        &lt;ManufacturerPartID&gt;12345678&lt;/ManufacturerPartID&gt;
        &lt;Extrinsic name="StockCode"&gt;23333&lt;/Extrinsic&gt;
        &lt;Extrinsic name="Quantity"&gt;1&lt;/Extrinsic&gt;
      &lt;/ItemDetail&gt;
    &lt;/ItemOut&gt;
  &lt;/OrderRequest&gt;
  </code></pre>
  
  <p>I tried with this code, but it's giving either null or object reference not set error:</p>
  
  <pre><code>XDocument xdoc = XDocument.Load(@"C:\PO.xml");
   var itemOut = (from c in xdoc.Descendants("OrderRequest")
  
                                 select new
                                 {
                                     SupplierID = c.Element("Money").Value                               ,
                                     Currency = c.Attribute("currency").Value,
                                     Money = c.Element("Money").Value,
                                     Description = c.Element("Description").Value,
                                     ManufacturerPartID = c.Element("ManufacturerPartID").Value,
                                     Extrinsic = c.Attribute("name").Value
                                 });
  
                  foreach (var element in itemOut)
                  {
                      Console.WriteLine(element.SupplierID);
                  }
  
                  Console.ReadLine();
  </code></pre>