C# 解析XML文档

C# 解析XML文档,c#,parsing,xpathdocument,C#,Parsing,Xpathdocument,我正在尝试解析来自Amazon AWS的远程XML文档: <ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2009-03-31"> <OperationRequest> <RequestId>011d32c5-4fab-4c7d-8785-ac48b9bda6da</RequestId> <Ar

我正在尝试解析来自Amazon 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>
我错过了什么?提前谢谢

编辑
我正在使用.net 3.5

您使用的是什么版本的.net framework?您可以使用LINQ吗?如果是的话,我会用它来提取这些值,因为这样做会更好一些。关于LINQ的一个非常好的例子,请看这里:


Amount不是一个属性,它是一个子元素

相应的xpath查询将是/ItemLookupResponse/Items/Item/ItemAttributes/ListPrice/Amount


您没有注意XML文档中的XML名称空间,也没有提供其他答案:

<ItemLookupResponse 
     xmlns="http://webservices.amazon.com/AWSECommerceService/2009-03-31">
更新:如果要浏览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;
       }
    }
}

遍历下的所有子节点并获取它们的名称和值。

我还没有准备好测试工具,所以我不知道这是否适用,但XPath对名称空间也很挑剔。我不知道XPathNavigator是否像代码中预期的那样自动处理默认名称空间。不过,我必须承认,我对xml的深层次细节不是很了解,这是行不通的,因为您没有考虑文档上的xml名称空间!你能在中帮助并编辑正确的版本吗?正如我所说,我对它背后的理论不是很了解。非常感谢。当我在var ListPrice上设置一个断点时——新资产净值。。。。。。通过它,我可以在结果视图集合中看到我需要的数据片段-我如何获得它?
<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);
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;
       }
    }
}