2010-06-14 177 views
0

我試圖解析遠程XML文檔(從亞馬遜AWS):解析XML文檔

<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2009-03-31"> 
     <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</DetailPageURL> 
      <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</URL> 
      </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</URL> 
      </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</URL> 
      </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</URL> 
      </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</URL> 
      </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</URL> 
      </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</URL> 
      </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> 

我試圖提取使用的XPathDocument從XML數據流,但沒有運氣:

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)); 
     } 

我錯過了什麼?提前致謝!!

編輯 我使用.NET 3.5

您正在使用哪種版本的.NET Framework的
+0

你看到了什麼錯誤? – psmears 2010-06-14 16:42:35

+0

沒有錯誤,但沒有數據。 – Neil 2010-06-14 17:00:30

回答

4

金額不是一個屬性,它是一個子元素。

根據xpath查詢將是/ItemLookupResponse/Items/Item/ItemAttributes/ListPrice/Amount

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

Response.Write(ListPrice.Value); 
+0

我沒有把測試工具放在一起,所以我不知道這是否適用,但XPath對命名空間也很挑剔。我不知道該代碼中XPathNavigator是否按照預期自動處理默認名稱空間。 – Toby 2010-06-14 16:56:36

+0

xpath是正確的(http://xpathvisualizer.codeplex.com/是測試它的好工具)。不過我必須承認,我並沒有深入xml的深層細節。 – Femaref 2010-06-14 17:01:08

+0

這不起作用,因爲您沒有考慮到文檔上的XML名稱空間! – 2010-06-14 19:31:03

2

你不注意的XML文檔中的XML命名空間(S)(既不是提供其他的答案):

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

你需要考慮到這一點!

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:,如果你想探索你ListPrice節點下面的子節點,你可以做這樣的事情:

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; 
     } 
    } 
} 

通過所有子節點迭代<ListPrice>下,並得到他們的名字和價值。

+0

當我在var ListPrice上放置一個斷點 - 新導航......並逐步完成時,我可以在結果視圖集合中看到需要的數據片段 - 我如何獲取它? – Neil 2010-06-14 17:57:57

+0

@Neil:更新了我的答案,展示瞭如何訪問細節.... – 2010-06-14 21:02:35