如何使用Visual Basic解析XDocument(Ebay)项目(列表)?

时间:2013-06-02 22:44:56

标签: vb.net parsing linq-to-xml

<code> 
        For Each oXElement In oXDocument.Descendants("searchResult")
              sTitle = oXElement.Element("title").Value
        Next
</code>

我也尝试过:

<code> 
       For Each oXElement In oXDocument.Elements(searchResults) 
           sTitle = oXElement.Element("title").Value 
        Next
</code>

我无法获取节点以及了解与XDocument节点通信的方式。

我的最终目标是从所有Ebay Element的属性创建一个Ebay对象模型。为此,我需要以某种方式引用XML标记 - 这是我非常感谢您的建议或示例示例,可以让我继续解析此XML响应。

非常感谢您的帮助。

PS:我已经搜索了类似的问题,发现了一些类似的但仍然无法使我的解析工作。

<findItemsByProductResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
  <ack>Success</ack>
  <version>1.12.0</version>
  <timestamp>2013-06-02T22:42:04.500Z</timestamp>
  <searchResult count="5">
    <item>
      <itemId>370821427802</itemId>
      <title>
Modern Database Management 11E by Hoffer, Ramesh, Topi 11th (Int'l Edition)
</title>
      <globalId>EBAY-US</globalId>
      <primaryCategory>
        <categoryId>2228</categoryId>
        <categoryName>Textbooks, Education</categoryName>
      </primaryCategory>
      <galleryURL>
http://thumbs3.ebaystatic.com/m/meSAqCRbXecSjZjO1833dWQ/140.jpg
</galleryURL>
      <viewItemURL>
http://www.ebay.com/itm/Modern-Database-Management-11E-Hoffer-Ramesh-Topi-11th-Intl-Edition-/370821427802?pt=US_Texbook_Education
</viewItemURL>
      <productId type="ReferenceID">143649496</productId>
      <paymentMethod>PayPal</paymentMethod>
      <autoPay>true</autoPay>
      <location>Malaysia</location>
      <country>MY</country>
      <shippingInfo>
        <shippingServiceCost currencyId="USD">0.0</shippingServiceCost>
        <shippingType>Free</shippingType>
        <shipToLocations>Worldwide</shipToLocations>
        <expeditedShipping>true</expeditedShipping>
        <oneDayShippingAvailable>false</oneDayShippingAvailable>
        <handlingTime>1</handlingTime>
      </shippingInfo>
      <sellingStatus>
        <currentPrice currencyId="USD">54.07</currentPrice>
        <convertedCurrentPrice currencyId="USD">54.07</convertedCurrentPrice>
        <sellingState>Active</sellingState>
        <timeLeft>P20DT10H47M20S</timeLeft>
      </sellingStatus>
      <listingInfo>
        <bestOfferEnabled>false</bestOfferEnabled>
        <buyItNowAvailable>false</buyItNowAvailable>
        <startTime>2013-05-24T09:25:25.000Z</startTime>
        <endTime>2013-06-23T09:29:24.000Z</endTime>
        <listingType>StoreInventory</listingType>
        <gift>false</gift>
      </listingInfo>
      <returnsAccepted>true</returnsAccepted>
      <condition>
        <conditionId>1000</conditionId>
        <conditionDisplayName>Brand New</conditionDisplayName>
      </condition>
      <isMultiVariationListing>false</isMultiVariationListing>
      <topRatedListing>true</topRatedListing>
    </item>
    <item>...</item>
    <item>...</item>
    <item>...</item>
    <item>...</item>
  </searchResult>
  <paginationOutput>
    <pageNumber>1</pageNumber>
    <entriesPerPage>5</entriesPerPage>
    <totalPages>3</totalPages>
    <totalEntries>14</totalEntries>
  </paginationOutput>
  <itemSearchURL>
http://www.ebay.com/ctg/143649496?LH_BIN=1&_ddo=1&_incaucbin=0&_ipg=5&_pgn=1
</itemSearchURL>
</findItemsByProductResponse>

2 个答案:

答案 0 :(得分:0)

查询XML时必须使用XNamespace实例:

Dim ns = XNamespace.Get("http://www.ebay.com/marketplace/search/v1/services")

然后将其添加到您发出的每个DescendantsElementsElementAttributesAttributes等来电:

For Each oXElement In oXDocument.Descendants(ns + "searchResult")
      sTitle = oXElement.Element(ns + "title").Value
Next

For Each oXElement In oXDocument.Elements(ns + searchResults) 
   sTitle = oXElement.Element(ns + "title").Value 
Next

答案 1 :(得分:0)

两件事。首先,你遇到了陷阱,它使用LINQ to XML捕获了90%的人。你忘记了命名空间。您可以使用以下适用于C#或VB的内容:

Dim ns = XNamespace.Get("http://www.ebay.com/marketplace/search/v1/services")

VB还允许您在命名空间中使用Imports,就像导入文件顶部的其他.Net命名空间一样。此选项的优点是,如果项目中有模式,则在构建查询时可以获得XML结构的智能感知。

Imports <xmlns:eb="http://www.ebay.com/marketplace/search/v1/services">

你遇到的第二个问题是title元素不是searchResult的直接子元素,而是更深层次嵌套。这是一个利用命名空间导入的示例。我正在使用VB XML Literals用于后代(...)与任何给你C#偏向答案的人形成对比; - )

Public Class XmlTest
    Public Sub TestXml()
        Dim data = <findItemsByProductResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
                       <ack>Success</ack>
                       <version>1.12.0</version>
                       <timestamp>2013-06-02T22:42:04.500Z</timestamp>
                       <searchResult count="5">
                           <item>
                               <itemId>370821427802</itemId>
                               <title>
Modern Database Management 11E by Hoffer, Ramesh, Topi 11th (Int'l Edition)
</title>
                               <globalId>EBAY-US</globalId>
                               <primaryCategory>
                                   <categoryId>2228</categoryId>
                                   <categoryName>Textbooks, Education</categoryName>
                               </primaryCategory>
                               <galleryURL>
http://thumbs3.ebaystatic.com/m/meSAqCRbXecSjZjO1833dWQ/140.jpg
</galleryURL>
                               <viewItemURL>
http://www.ebay.com/itm/Modern-Database-Management-11E-Hoffer-Ramesh-Topi-11th-Intl-Edition-/370821427802?pt=US_Texbook_Education
</viewItemURL>
                               <productId type="ReferenceID">143649496</productId>
                               <paymentMethod>PayPal</paymentMethod>
                               <autoPay>true</autoPay>
                               <location>Malaysia</location>
                               <country>MY</country>
                               <shippingInfo>
                                   <shippingServiceCost currencyId="USD">0.0</shippingServiceCost>
                                   <shippingType>Free</shippingType>
                                   <shipToLocations>Worldwide</shipToLocations>
                                   <expeditedShipping>true</expeditedShipping>
                                   <oneDayShippingAvailable>false</oneDayShippingAvailable>
                                   <handlingTime>1</handlingTime>
                               </shippingInfo>
                               <sellingStatus>
                                   <currentPrice currencyId="USD">54.07</currentPrice>
                                   <convertedCurrentPrice currencyId="USD">54.07</convertedCurrentPrice>
                                   <sellingState>Active</sellingState>
                                   <timeLeft>P20DT10H47M20S</timeLeft>
                               </sellingStatus>
                               <listingInfo>
                                   <bestOfferEnabled>false</bestOfferEnabled>
                                   <buyItNowAvailable>false</buyItNowAvailable>
                                   <startTime>2013-05-24T09:25:25.000Z</startTime>
                                   <endTime>2013-06-23T09:29:24.000Z</endTime>
                                   <listingType>StoreInventory</listingType>
                                   <gift>false</gift>
                               </listingInfo>
                               <returnsAccepted>true</returnsAccepted>
                               <condition>
                                   <conditionId>1000</conditionId>
                                   <conditionDisplayName>Brand New</conditionDisplayName>
                               </condition>
                               <isMultiVariationListing>false</isMultiVariationListing>
                               <topRatedListing>true</topRatedListing>
                           </item>
                           <item>...</item>
                           <item>...</item>
                           <item>...</item>
                           <item>...</item>
                       </searchResult>
                       <paginationOutput>
                           <pageNumber>1</pageNumber>
                           <entriesPerPage>5</entriesPerPage>
                           <totalPages>3</totalPages>
                           <totalEntries>14</totalEntries>
                       </paginationOutput>
                   </findItemsByProductResponse>

        For Each el In data...<eb:searchResult>
            Console.WriteLine(el...<eb:title>.Value)
        Next


    End Sub
End Class