views:

63

answers:

3

I am trying to parse a remote XML document (from Amazon AWS):

<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2009-03-31"&gt;
      <OperationRequest>
        <RequestId>011d32c5-4fab-4c7d-8785-ac48b9bda6da</RequestId>
        <Arguments>
          <Argument Name="Condition" Value="New"></Argument>
          <Argument Name="Operation" Value="ItemLookup"></Argument>
          <Argument Name="Service" Value="AWSECommerceService"></Argument>
          <Argument Name="Signature" Value="73l8oLJhITTsWtHxsdrS3BMKsdf01n37PE8u/XCbsJM="></Argument>
          <Argument Name="MerchantId" Value="Amazon"></Argument>
          <Argument Name="Version" Value="2009-03-31"></Argument>
          <Argument Name="ItemId" Value="603084260089"></Argument>
          <Argument Name="IdType" Value="UPC"></Argument>
          <Argument Name="AWSAccessKeyId" Value="[myAccessKey]"></Argument>
          <Argument Name="Timestamp" Value="2010-06-14T15:03:27Z"></Argument>
          <Argument Name="ResponseGroup" Value="OfferSummary,ItemAttributes"></Argument>
          <Argument Name="SearchIndex" Value="All"></Argument>
        </Arguments>
        <RequestProcessingTime>0.0318510000000000</RequestProcessingTime>
      </OperationRequest>
      <Items>
        <Request>
          <IsValid>True</IsValid>
          <ItemLookupRequest>
            <Condition>New</Condition>
            <DeliveryMethod>Ship</DeliveryMethod>
            <IdType>UPC</IdType>
            <MerchantId>Amazon</MerchantId>
            <OfferPage>1</OfferPage>
            <ItemId>603084260089</ItemId>
            <ResponseGroup>OfferSummary</ResponseGroup>
            <ResponseGroup>ItemAttributes</ResponseGroup>
            <ReviewPage>1</ReviewPage>
            <ReviewSort>-SubmissionDate</ReviewSort>
            <SearchIndex>All</SearchIndex>
            <VariationPage>All</VariationPage>
          </ItemLookupRequest>
        </Request>
        <Item>
          <ASIN>B0000UTUNI</ASIN>
          <DetailPageURL>http://www.amazon.com/Garnier-Fructis-Fortifying-Conditioner-Minute/dp/B0000UTUNI%3FSubscriptionId%3DAKIAIYPTKHCWTRWWPWBQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0000UTUNI&lt;/DetailPageURL&gt;
          <ItemLinks>
            <ItemLink>
              <Description>Technical Details</Description>
              <URL>http://www.amazon.com/Garnier-Fructis-Fortifying-Conditioner-Minute/dp/tech-data/B0000UTUNI%3FSubscriptionId%3DAKIAIYPTKHCWTRWWPWBQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0000UTUNI&lt;/URL&gt;
            </ItemLink>
            <ItemLink>
              <Description>Add To Baby Registry</Description>
              <URL>http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3DB0000UTUNI%26SubscriptionId%3DAKIAIYPTKHCWTRWWPWBQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0000UTUNI&lt;/URL&gt;
            </ItemLink>
            <ItemLink>
              <Description>Add To Wedding Registry</Description>
              <URL>http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3DB0000UTUNI%26SubscriptionId%3DAKIAIYPTKHCWTRWWPWBQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0000UTUNI&lt;/URL&gt;
            </ItemLink>
            <ItemLink>
              <Description>Add To Wishlist</Description>
              <URL>http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3DB0000UTUNI%26SubscriptionId%3DAKIAIYPTKHCWTRWWPWBQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0000UTUNI&lt;/URL&gt;
            </ItemLink>
            <ItemLink>
              <Description>Tell A Friend</Description>
              <URL>http://www.amazon.com/gp/pdp/taf/B0000UTUNI%3FSubscriptionId%3DAKIAIYPTKHCWTRWWPWBQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0000UTUNI&lt;/URL&gt;
            </ItemLink>
            <ItemLink>
              <Description>All Customer Reviews</Description>
              <URL>http://www.amazon.com/review/product/B0000UTUNI%3FSubscriptionId%3DAKIAIYPTKHCWTRWWPWBQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0000UTUNI&lt;/URL&gt;
            </ItemLink>
            <ItemLink>
              <Description>All Offers</Description>
              <URL>http://www.amazon.com/gp/offer-listing/B0000UTUNI%3FSubscriptionId%3DAKIAIYPTKHCWTRWWPWBQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0000UTUNI&lt;/URL&gt;
            </ItemLink>
          </ItemLinks>
          <ItemAttributes>
            <Binding>Health and Beauty</Binding>
            <Brand>Garnier</Brand>
            <EAN>0603084260089</EAN>
            <Feature>Helps restore strength and shine</Feature>
            <Feature>Penetrates deep to nourish, repair and rejuvenate</Feature>
            <Feature>Makes hair softer and more manageable without weighing it down</Feature>
            <ItemDimensions>
              <Weight Units="hundredths-pounds">40</Weight>
            </ItemDimensions>
            <Label>Garnier</Label>
            <ListPrice>
              <Amount>419</Amount>
              <CurrencyCode>USD</CurrencyCode>
              <FormattedPrice>$4.19</FormattedPrice>
            </ListPrice>
            <Manufacturer>Garnier</Manufacturer>
            <NumberOfItems>1</NumberOfItems>
            <ProductGroup>Health and Beauty</ProductGroup>
            <ProductTypeName>ABIS_DRUGSTORE</ProductTypeName>
            <Publisher>Garnier</Publisher>
            <Size>5.0 oz</Size>
            <Studio>Garnier</Studio>
            <Title>Garnier Fructis Fortifying Fortifying Deep Conditioner, 3 Minute Masque - 5 oz</Title>
            <UPC>603084260089</UPC>
          </ItemAttributes>
          <OfferSummary>
            <LowestNewPrice>
              <Amount>229</Amount>
              <CurrencyCode>USD</CurrencyCode>
              <FormattedPrice>$2.29</FormattedPrice>
            </LowestNewPrice>
            <TotalNew>7</TotalNew>
            <TotalUsed>0</TotalUsed>
            <TotalCollectible>0</TotalCollectible>
            <TotalRefurbished>0</TotalRefurbished>
          </OfferSummary>
        </Item>
      </Items>
    </ItemLookupResponse>

I am trying to extract data from the XML stream using XPathDocument, but with no luck:

WebRequest request = HttpWebRequest.Create(url);
        WebResponse response = request.GetResponse();
        //XmlDocument doc = new XmlDocument();

        XPathDocument Doc = new XPathDocument(response.GetResponseStream());
        XPathNavigator nav = Doc.CreateNavigator();
        XPathNodeIterator ListPrice = nav.Select("/ItemLookupResponse/Items/Item/ItemAttributes/ListPrice");
        foreach (XPathNavigator node in ListPrice)
        {
            Response.Write(node.GetAttribute("Amount", NAMESPACE));
        }

What am I missing? Thanks in advance!!

EDIT I am using .net 3.5

+4  A: 

What version of the .Net framework are you using? Is LINQ available to you? If it is I would use that to pull out the values as it is a little nicer to work with. For a really nice example of LINQ have a look here:

http://weblogs.asp.net/scottgu/archive/2007/08/07/using-linq-to-xml-and-how-to-build-a-custom-rss-feed-reader-with-it.aspx

DeanMc
+4  A: 

Amount is not an attribute, it's a child element.

The according xpath-query would be /ItemLookupResponse/Items/Item/ItemAttributes/ListPrice/Amount.

XPathNodeIterator ListPrice = nav.Select("/ItemLookupResponse/Items/Item/ItemAttributes/ListPrice/Amount");

Response.Write(ListPrice.Value);
Femaref
I haven't throw together a test harness, so I don't know if this applies, but XPath is also picky about namespaces. I don't know if the XPathNavigator automagically handles a default namespace as expected in this code.
Toby
The xpath is correct (http://xpathvisualizer.codeplex.com/ is a great tool for testing it). I have to admit though, I'm not very into the deeper details of xml.
Femaref
That won't work, since you're not taking into account the XML namespace on the document!
marc_s
could you help and edit the right version in? As I said, I'm not that well into the theory behind it. Would appreciate it.
Femaref
+2  A: 

You're not paying attention to the XML namespace(s) in the XML document (and neither are the other answers provided):

<ItemLookupResponse 
     xmlns="http://webservices.amazon.com/AWSECommerceService/2009-03-31"&gt;

You need to take that into account!

XPathDocument Doc = new XPathDocument(@"D:\amazon.xml");

XPathNavigator nav = Doc.CreateNavigator();

// add a XML namespace manager - pick any prefix you want        
XmlNamespaceManager mngr = new XmlNamespaceManager(nav.NameTable);
mngr.AddNamespace("az", "http://webservices.amazon.com/AWSECommerceService/2009-03-31");

// use that XML namespace prefix to select
var ListPrice = nav.Select("/az:ItemLookupResponse/az:Items/az:Item/az:ItemAttributes/az:ListPrice", mngr);

UPDATE: if you want to explore the child nodes below your ListPrice node, you can do something like this:

var curr = ListPrice.Current;

if(ListPrice.MoveNext())
{
    var lpc = ListPrice.Current;

    if(lpc.MoveToFirstChild())
    {
       var node = lpc.Name + "/" + lpc.Value;

       while(lpc.MoveToNext())
       {
           node = lpc.Name + "/" + lpc.Value;
       }
    }
}

to iterate through all child nodes under <ListPrice> and get their name and value.

marc_s
When I put a breakpoint on var ListPrice - new nav...... and step through it, I can see the piece of data I need in the results view collection - how do I get it?
Neil
@Neil: updated my answer showing how to access the details....
marc_s