Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 http://www.codeproject.com/Articles/30965/Read-XML-with-Namespace-resolution-using-XLinq-XEl 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?

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.

C# 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);

XML Data:

<ns2:ItemAttributes xml:lang="en-US" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
  <ns2:Author>Troelsen, Andrew</ns2:Author>
  <ns2:Binding>Paperback</ns2:Binding>
  <ns2:Brand>Apress</ns2:Brand>
  <ns2:Edition>5</ns2:Edition>
  <ns2:ItemDimensions>
    <ns2:Height Units="inches">9.21</ns2:Height>
    <ns2:Length Units="inches">7.48</ns2:Length>
    <ns2:Width Units="inches">2.52</ns2:Width>
    <ns2:Weight Units="pounds">5.80</ns2:Weight>
  </ns2:ItemDimensions>
  <ns2:IsAutographed>false</ns2:IsAutographed>
  <ns2:IsEligibleForTradeIn>true</ns2:IsEligibleForTradeIn>
  <ns2:IsMemorabilia>false</ns2:IsMemorabilia>
  <ns2:Label>Apress</ns2:Label>
  <ns2:Languages>
    <ns2:Language>
      <ns2:Name>english</ns2:Name>
      <ns2:Type>Unknown</ns2:Type>
    </ns2:Language>
    <ns2:Language>
      <ns2:Name>english</ns2:Name>
      <ns2:Type>Original Language</ns2:Type>
    </ns2:Language>
    <ns2:Language>
      <ns2:Name>english</ns2:Name>
      <ns2:Type>Published</ns2:Type>
    </ns2:Language>
  </ns2:Languages>
  <ns2:ListPrice>
    <ns2:Amount>59.99</ns2:Amount>
    <ns2:CurrencyCode>USD</ns2:CurrencyCode>
  </ns2:ListPrice>
  <ns2:Manufacturer>Apress</ns2:Manufacturer>
  <ns2:NumberOfItems>1</ns2:NumberOfItems>
  <ns2:NumberOfPages>1752</ns2:NumberOfPages>
  <ns2:PackageDimensions>
    <ns2:Height Units="inches">2.60</ns2:Height>
    <ns2:Length Units="inches">9.20</ns2:Length>
    <ns2:Width Units="inches">7.50</ns2:Width>
    <ns2:Weight Units="pounds">5.80</ns2:Weight>
  </ns2:PackageDimensions>
  <ns2:PartNumber>9781430225492</ns2:PartNumber>
  <ns2:ProductGroup>Book</ns2:ProductGroup>
  <ns2:ProductTypeName>ABIS_BOOK</ns2:ProductTypeName>
  <ns2:PublicationDate>2010-05-14</ns2:PublicationDate>
  <ns2:Publisher>Apress</ns2:Publisher>
  <ns2:SmallImage>
    <ns2:URL>http://ecx.images-amazon.com/images/I/51h9Sju5NKL._SL75_.jpg</ns2:URL>
    <ns2:Height Units="pixels">75</ns2:Height>
    <ns2:Width Units="pixels">61</ns2:Width>
  </ns2:SmallImage>
  <ns2:Studio>Apress</ns2:Studio>
  <ns2:Title>Pro C# 2010 and the .NET 4 Platform</ns2:Title>
</ns2:ItemAttributes>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Firstly: element name is Height and not Heigth.

You can use this code to get Height value:

var doc = XDocument.Load(path);

XNamespace ns2 = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd";


string val = doc.Document.Descendants(ns2 + "ItemDimensions")
    .FirstOrDefault().Element(ns2 + "Height").Value;

Console.WriteLine(val);
share|improve this answer
 
Thank you this help out a lot. Actually I can't believe I spelled Height wrong. It's been a long day and I am on last burst of energy. –  Brandon Wilson Oct 24 '12 at 0:07
 
@BrandonWilson, You're welcome! –  Kirill Polishchuk Oct 24 '12 at 0:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.