XML : Find Xml Node by Node Value

I have created a project to read the Amazon Product Advertising API, retrieving an XML document using the following code:

  WebRequest request = HttpWebRequest.Create(signedurl);  WebResponse responseStream = request.GetResponse();  XmlDocument doc = new XmlDocument();  doc.Load(responseStream.GetResponseStream());    

And, through further research I have been able to access element values using the following;

  XmlNode Item_IdNode = doc.GetElementsByTagName("ASIN").Item(0);  XmlNode PriceNode = doc.GetElementsByTagName("FormattedPrice").Item(3);    string F_Price = PriceNode.InnerText;  string xml_ItemId = Item_IdNode.InnerText;    

This code work fine for accessing a product ASIN and price for a single record, but I would like to retrieve up to 10 records per request.

So far, I know I can increment the "Item(0)" to page through the other item ASIN's, but the "FormattedPrice" element is repeated many times per product, and may not necessarily appear at point 6,9,12 for the other product records.

For each 10 records retrieved, I already know (use) the ASIN (unique product reference) in the api call to select the specific records.

Where I would like to progress the code to is to "search" the XML response for item ASIN "12345", then drill down to the specific node "OfferSummary/LowestNewPrice/FormattedPrice" to retrieve the item price into a variable, and so on for all other ASIN's item prices.

Here is an extract of the first two XML item records (if this helps).

      <Items>        <Request>        <IsValid>True</IsValid>        <ItemLookupRequest>              <Item><ASIN>12345</ASIN>                    <OfferSummary>                              <LowestNewPrice>                              <Amount>1098</Amount>                              <CurrencyCode>GBP</CurrencyCode>                              <FormattedPrice>£10.98</FormattedPrice>                              </LowestNewPrice>                     </OfferSummary>                     .                     .                     .                     .      <Items>        <Request>        <IsValid>True</IsValid>        <ItemLookupRequest>              <Item><ASIN>23456</ASIN>                    <OfferSummary>                              <LowestNewPrice>                              <Amount>1098</Amount>                              <CurrencyCode>GBP</CurrencyCode>                              <FormattedPrice>£10.98</FormattedPrice>                              </LowestNewPrice>                     </OfferSummary>                     .                     .                     .                     .    

I have some asp.net experience, but have not used XML 'readers' before and would be grateful for any pointers in the right direction on 'searching' the XML file for each ASIN, and its corresponding 'FormattedPrice'.

Hope this is sufficient information, please let me know if any further information is needed.

Many thanks, James

No comments:

Post a Comment