XML文档中的特定信息

时间:2014-12-22 20:07:13

标签: xml vb.net

我试图解析XML数据并且很难做到这一点。这是XML部分

<?xml version="1.0"?> <GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"> <GetLowestOfferListingsForASINResult ASIN="b0091raue4" status="Success">   <AllOfferListingsConsidered>true</AllOfferListingsConsidered>   <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
        <ASIN>b0091raue4</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <LowestOfferListings>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>New</ItemCondition>
          <ItemSubcondition>New</ItemSubcondition>
          <FulfillmentChannel>Amazon</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>7</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>723</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>28.70</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>28.70</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
      <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>New</ItemCondition>
          <ItemSubcondition>New</ItemSubcondition>
          <FulfillmentChannel>Amazon</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>27</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>29.00</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>29.00</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>


     <LowestOfferListing>
        <Qualifiers>
          <ItemCondition>Collectible</ItemCondition>
          <ItemSubcondition>Mint</ItemSubcondition>
          <FulfillmentChannel>Merchant</FulfillmentChannel>
          <ShipsDomestically>True</ShipsDomestically>
          <ShippingTime>
            <Max>0-2 days</Max>
          </ShippingTime>
          <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating>
        </Qualifiers>
        <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
        <SellerFeedbackCount>1138</SellerFeedbackCount>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>129.99</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>125.00</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>4.99</Amount>
          </Shipping>
        </Price>
        <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
      </LowestOfferListing>
    </LowestOfferListings>
  </Product>
</GetLowestOfferListingsForASINResult>
<ResponseMetadata>
  <RequestId>ba623b4e-338b-4bc7-9ee0-9fd2e20489d3</RequestId>
</ResponseMetadata>
</GetLowestOfferListingsForASINResponse>

我想循环遍历所有&#34; LowestOfferListing&#34;节点只返回&#34; ItemCondition&#34;和&#34;金额&#34;。

我尝试将数据放入XML文档,数据集和数据表中。这是我在的地方:

        Dim xml As New Xml.XmlDocument()
        xml.LoadXml(responseBody)
        Dim ds As New DataSet
        ds.ReadXml(New StringReader(responseBody))

有没有人知道如何循环并退出这两个值?

2 个答案:

答案 0 :(得分:2)

LINQ to XML为查询XML提供了一个很好的API,特别是在支持XML文字支持的VB.NET中。例如这将显示ItemCondition和Amount(来自ListingPrice)字段。

更新您需要在下面添加XML命名空间导入才能使其正常运行。

Imports System.Xml.Linq
' Import the XML namespace for the elements being queried
Imports <xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
'...
Dim xml = XElement.Parse(responseBody)
Dim result =
    From item In xml...<LowestOfferListing>
    Select
        Condition = item.<Qualifiers>.<ItemCondition>.Value,
        Amount = item.<Price>.<ListingPrice>.<Amount>.Value
For Each item In result
    Console.WriteLine("{0} - {1}", item.Condition, item.Amount)
Next

答案 1 :(得分:0)

你应该使用XPath:

Dim xpathDoc As XPathDocument
Dim xmlNav As XPathNavigator
Dim xmlNI As XPathNodeIterator
xpathDoc = New XPathDocument("c:\builder.xml")
xmlNav = xpathDoc.CreateNavigator()
xmlNI = xmlNav.Select("/LowestOfferListings/LowestOfferListing/Qualifiers/ItemCondition")
While (xmlNI.MoveNext())
  System.Console.WriteLine(xmlNI.Current.Name + " : " + xmlNI.Current.Value)
End While

xpath应该与您对xml感兴趣的元素的嵌套匹配。