locked
Find Xml Node by Parent Node Value RRS feed

  • Question

  • User-756030211 posted

    Hi, 

    I am trying to create a small application to read the XML data from the Amazon Product Advertising API.  I would like to be able to retrieve the 'FormattedPrice' for a given product ASIN in the Amazon XML response.

    So far, I can use the following code to access the 'FormattedPrice' in my sample XML file.

    var res = XElement.Load(Server.MapPath("/App_Data/AWSS.xml"))
    .Descendants("ASIN").FirstOrDefault(elem => elem.Value == "B001MS70F2")
    .Parent.Descendants("FormattedPrice").Select(elem => elem.Value)
    .FirstOrDefault();
    Response.Write(res);

    But the code doesn't work on the 'full' XML response from Amazon, as below.

    <ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
    <OperationRequest>
    <HTTPHeaders>
    <Header Name="UserAgent" Value="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"/>
    </HTTPHeaders>
    <RequestId>#############</RequestId>
    <Arguments>
    <Argument Name="AWSAccessKeyId" Value="#############"/>
    <Argument Name="AssociateTag" Value="#############"/>
    <Argument Name="IdType" Value="ASIN"/>
    <Argument Name="ItemId" Value="B001MS70F2,B007W1RSZA"/>
    <Argument Name="Operation" Value="ItemLookup"/>
    <Argument Name="ResponseGroup" Value="Offers"/>
    <Argument Name="Service" Value="AWSECommerceService"/>
    <Argument Name="Timestamp" Value="2016-01-06T23:01:02Z"/>
    <Argument Name="Signature" Value="#############"/>
    </Arguments>
    <RequestProcessingTime>0.0209750000000000</RequestProcessingTime>
    </OperationRequest>
    <Items>
    <Request>
    <IsValid>True</IsValid>
    <ItemLookupRequest>
    <IdType>ASIN</IdType>
    <ItemId>B001MS70F2</ItemId>
    <ItemId>B007W1RSZA</ItemId>
    <ResponseGroup>Offers</ResponseGroup>
    <VariationPage>All</VariationPage>
    </ItemLookupRequest>
    </Request>
    <Item>
    <ASIN>B001MS70F2</ASIN>
    <ParentASIN>B019IGAHUY</ParentASIN>
    <OfferSummary>
    <LowestNewPrice>
    <Amount>1049</Amount>
    <CurrencyCode>GBP</CurrencyCode>
    <FormattedPrice>£10.49</FormattedPrice>
    </LowestNewPrice>
    <TotalNew>32</TotalNew>
    <TotalUsed>0</TotalUsed>
    <TotalCollectible>0</TotalCollectible>
    <TotalRefurbished>0</TotalRefurbished>
    </OfferSummary>
    <Offers>
    <TotalOffers>1</TotalOffers>
    <TotalOfferPages>1</TotalOfferPages>
    <MoreOffersUrl>
    http://www.amazon.co.uk/gp/offer-listing/B001MS70F2%3FSubscriptionId%3DAKIAI4V5X2Q7F3BOU7MA%26tag%3Dbusin02-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12734%26creativeASIN%3DB001MS70F2
    </MoreOffersUrl>
    <Offer>
    <OfferAttributes>
    <Condition>New</Condition>
    </OfferAttributes>
    <OfferListing>
    <OfferListingId>
    VbyiDUr1A7VXNun65VvEF8WmWG3ZOzirk%2BGjIdOOGBB38lLlcRYaEKyl4pS6hdrqhZuOLqfW4uTVLtqsCUfanWyEaltghotq
    </OfferListingId>
    <Price>
    <Amount>1049</Amount>
    <CurrencyCode>GBP</CurrencyCode>
    <FormattedPrice>£10.49</FormattedPrice>
    </Price>
    <AmountSaved>
    <Amount>284</Amount>
    <CurrencyCode>GBP</CurrencyCode>
    <FormattedPrice>£2.84</FormattedPrice>
    </AmountSaved>
    <PercentageSaved>21</PercentageSaved>
    <Availability>Usually dispatched within 24 hours</Availability>
    <AvailabilityAttributes>
    <AvailabilityType>now</AvailabilityType>
    <MinimumHours>0</MinimumHours>
    <MaximumHours>0</MaximumHours>
    </AvailabilityAttributes>
    <IsEligibleForSuperSaverShipping>1</IsEligibleForSuperSaverShipping>
    <IsEligibleForPrime>1</IsEligibleForPrime>
    </OfferListing>
    </Offer>
    </Offers>
    </Item>
    <Item>
    <ASIN>B007W1RSZA</ASIN>
    <OfferSummary>
    <LowestNewPrice>
    <Amount>1630</Amount>
    <CurrencyCode>GBP</CurrencyCode>
    <FormattedPrice>£16.30</FormattedPrice>
    </LowestNewPrice>
    <TotalNew>7</TotalNew>
    <TotalUsed>0</TotalUsed>
    <TotalCollectible>0</TotalCollectible>
    <TotalRefurbished>0</TotalRefurbished>
    </OfferSummary>
    <Offers>
    <TotalOffers>1</TotalOffers>
    <TotalOfferPages>1</TotalOfferPages>
    <MoreOffersUrl>
    http://www.amazon.co.uk/gp/offer-listing/B007W1RSZA%3FSubscriptionId%3DAKIAI4V5X2Q7F3BOU7MA%26tag%3Dbusin02-21%26linkCode%3Dxm2%26camp%3D2025%26creative%3D12734%26creativeASIN%3DB007W1RSZA
    </MoreOffersUrl>
    <Offer>
    <OfferAttributes>
    <Condition>New</Condition>
    </OfferAttributes>
    <OfferListing>
    <OfferListingId>
    VbyiDUr1A7VXNun65VvEF74FLb5xO3BqKAr7e2DBjtDJNt3ZXQTDuPuGzaLdifzl2xEs1x4swNRE3U6yP3JfXvjWXxBDUM1vGxR8eaR1suappuRh5ZARKbjoGHx3NvEpVdrLfxvwmIzXoFSSq50uWg%3D%3D
    </OfferListingId>
    <Price>
    <Amount>1630</Amount>
    <CurrencyCode>GBP</CurrencyCode>
    <FormattedPrice>£16.30</FormattedPrice>
    </Price>
    <Availability>Usually dispatched within 1-2 business days</Availability>
    <AvailabilityAttributes>
    <AvailabilityType>now</AvailabilityType>
    <MinimumHours>24</MinimumHours>
    <MaximumHours>48</MaximumHours>
    </AvailabilityAttributes>
    <IsEligibleForSuperSaverShipping>0</IsEligibleForSuperSaverShipping>
    <IsEligibleForPrime>0</IsEligibleForPrime>
    </OfferListing>
    </Offer>
    </Offers>
    </Item>
    </Items>
    </ItemLookupResponse>

    My 'local' sample XML file doesn't contain the 'ItemLookupResponse' and 'OperationRequest', just the XML nodes between the <ITEMS> node. So I believe I need to change my code above to take into account the 'root' XML nodes.

    By copying the full Amazon XML response (shown above) to my local XML data file, and removing the 'ItemLookupResponse' node gives me the error 'multiple root elements', but removing the 'ItemLookupResponse' and 'OperationRequest' gives me working code retrieving the 'FormattedPrice' for the give product ASIN.

    Using my 'query' (above) on the full XML response gives me the error 'Object reference not set to an instance of an object.'

    I would be very grateful if anyone could point me in the right direction to run my adapted 'query' on the full XML, and overcome the 'Object reference not set to an instance of an object' error.

    Tuesday, January 12, 2016 8:57 PM

Answers

  • User614698185 posted

    Hi Jamez401,

    Welcome to ASP.NET Forums!

    I think you could create an XmlDocument and load your xml items' value, then use OuterXml property to convert XmlDocument to a string. The OuterXml property returns a string version of the xml:

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(""); //xml items
    
    XmlNode root = doc.DocumentElement;
    
    // OuterXml includes the markup of current node.
    return root.OuterXml;

    Or you could use StringBuilder to get the nodes:

    http://www.java2s.com/Tutorial/ASP.NET/0500__XML/GetNodebyname.htm

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 13, 2016 7:37 AM