解析 XML 文档
我正在尝试解析远程 XML 文档(来自 Amazon AWS):
<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
I am trying to parse a remote XML document (from Amazon AWS):
<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>
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
amount 不是一个属性,它是一个子元素。
相应的 xpath-query 将为
/ItemLookupResponse/Items/Item/ItemAttributes/ListPrice/Amount
。Amount is not an attribute, it's a child element.
The according xpath-query would be
/ItemLookupResponse/Items/Item/ItemAttributes/ListPrice/Amount
.您使用什么版本的 .Net 框架?您可以使用 LINQ 吗?如果是的话,我会用它来提取值,因为它使用起来更好一些。有关 LINQ 的一个非常好的示例,请查看此处:
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
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
您没有注意 XML 文档中的 XML 命名空间(也没有提供其他答案):
您需要考虑到这一点!
更新:如果您想探索
ListPrice
节点下的子节点,您可以执行以下操作:迭代
下的所有子节点
并获取它们的名称和值。You're not paying attention to the XML namespace(s) in the XML document (and neither are the other answers provided):
You need to take that into account!
UPDATE: if you want to explore the child nodes below your
ListPrice
node, you can do something like this:to iterate through all child nodes under
<ListPrice>
and get their name and value.