title: Parsing XML with Namespaces C#
body: <p>I have been working on parsing this XML data all day. It seems like my problem is namespaces. I thought a had a glimmer of hope with my last website <a href="http://www.codeproject.com/Articles/30965/Read-XML-with-Namespace-resolution-using-XLinq-XEl" rel="nofollow">http://www.codeproject.com/Articles/30965/Read-XML-with-Namespace-resolution-using-XLinq-XEl</a> but it failed as well. Each time I run the code below I get "Object reference not set to an instance of an object." There seems to be many ways of parsing XML with C# but these namespaces are killing me. What am I doing wrong?</p>
  
  <p>Do keep in mind I am new to XML and C# and use to parsing JSON data. And yes I have Googled several way to parse XML data and nothing has be useful in this case. I can get XML without the namespaces to work but with them always has that error.</p>
  
  <p>C# Code:</p>
  
  <pre><code> XElement element = XElement.Parse(xml);
  
   XNamespace ns2 = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd";
   string val = element.Element(ns2 + "ItemDimensions").Element(ns2 + "Heigth").Value;
                                  Console.WriteLine(val);
  </code></pre>
  
  <p>XML Data:</p>
  
  <pre><code>&lt;ns2:ItemAttributes xml:lang="en-US" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"&gt;
    &lt;ns2:Author&gt;Troelsen, Andrew&lt;/ns2:Author&gt;
    &lt;ns2:Binding&gt;Paperback&lt;/ns2:Binding&gt;
    &lt;ns2:Brand&gt;Apress&lt;/ns2:Brand&gt;
    &lt;ns2:Edition&gt;5&lt;/ns2:Edition&gt;
    &lt;ns2:ItemDimensions&gt;
      &lt;ns2:Height Units="inches"&gt;9.21&lt;/ns2:Height&gt;
      &lt;ns2:Length Units="inches"&gt;7.48&lt;/ns2:Length&gt;
      &lt;ns2:Width Units="inches"&gt;2.52&lt;/ns2:Width&gt;
      &lt;ns2:Weight Units="pounds"&gt;5.80&lt;/ns2:Weight&gt;
    &lt;/ns2:ItemDimensions&gt;
    &lt;ns2:IsAutographed&gt;false&lt;/ns2:IsAutographed&gt;
    &lt;ns2:IsEligibleForTradeIn&gt;true&lt;/ns2:IsEligibleForTradeIn&gt;
    &lt;ns2:IsMemorabilia&gt;false&lt;/ns2:IsMemorabilia&gt;
    &lt;ns2:Label&gt;Apress&lt;/ns2:Label&gt;
    &lt;ns2:Languages&gt;
      &lt;ns2:Language&gt;
        &lt;ns2:Name&gt;english&lt;/ns2:Name&gt;
        &lt;ns2:Type&gt;Unknown&lt;/ns2:Type&gt;
      &lt;/ns2:Language&gt;
      &lt;ns2:Language&gt;
        &lt;ns2:Name&gt;english&lt;/ns2:Name&gt;
        &lt;ns2:Type&gt;Original Language&lt;/ns2:Type&gt;
      &lt;/ns2:Language&gt;
      &lt;ns2:Language&gt;
        &lt;ns2:Name&gt;english&lt;/ns2:Name&gt;
        &lt;ns2:Type&gt;Published&lt;/ns2:Type&gt;
      &lt;/ns2:Language&gt;
    &lt;/ns2:Languages&gt;
    &lt;ns2:ListPrice&gt;
      &lt;ns2:Amount&gt;59.99&lt;/ns2:Amount&gt;
      &lt;ns2:CurrencyCode&gt;USD&lt;/ns2:CurrencyCode&gt;
    &lt;/ns2:ListPrice&gt;
    &lt;ns2:Manufacturer&gt;Apress&lt;/ns2:Manufacturer&gt;
    &lt;ns2:NumberOfItems&gt;1&lt;/ns2:NumberOfItems&gt;
    &lt;ns2:NumberOfPages&gt;1752&lt;/ns2:NumberOfPages&gt;
    &lt;ns2:PackageDimensions&gt;
      &lt;ns2:Height Units="inches"&gt;2.60&lt;/ns2:Height&gt;
      &lt;ns2:Length Units="inches"&gt;9.20&lt;/ns2:Length&gt;
      &lt;ns2:Width Units="inches"&gt;7.50&lt;/ns2:Width&gt;
      &lt;ns2:Weight Units="pounds"&gt;5.80&lt;/ns2:Weight&gt;
    &lt;/ns2:PackageDimensions&gt;
    &lt;ns2:PartNumber&gt;9781430225492&lt;/ns2:PartNumber&gt;
    &lt;ns2:ProductGroup&gt;Book&lt;/ns2:ProductGroup&gt;
    &lt;ns2:ProductTypeName&gt;ABIS_BOOK&lt;/ns2:ProductTypeName&gt;
    &lt;ns2:PublicationDate&gt;2010-05-14&lt;/ns2:PublicationDate&gt;
    &lt;ns2:Publisher&gt;Apress&lt;/ns2:Publisher&gt;
    &lt;ns2:SmallImage&gt;
      &lt;ns2:URL&gt;http://ecx.images-amazon.com/images/I/51h9Sju5NKL._SL75_.jpg&lt;/ns2:URL&gt;
      &lt;ns2:Height Units="pixels"&gt;75&lt;/ns2:Height&gt;
      &lt;ns2:Width Units="pixels"&gt;61&lt;/ns2:Width&gt;
    &lt;/ns2:SmallImage&gt;
    &lt;ns2:Studio&gt;Apress&lt;/ns2:Studio&gt;
    &lt;ns2:Title&gt;Pro C# 2010 and the .NET 4 Platform&lt;/ns2:Title&gt;
  &lt;/ns2:ItemAttributes&gt;
  </code></pre>