0
<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 元素的属性中创建一个 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>
4

2 回答 2

0

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

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

然后将其添加到您进行的每个Descendants, Elements, Element, Attributes,Attributes等调用中:

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
于 2013-08-16T14:07:19.333 回答
0

两件事情。首先,您落入了一个陷阱,该陷阱使 90% 的人在使用 LINQ to XML 时遇到问题。你忘记了命名空间。您可以使用在 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 for descendents (...) 与任何给你 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
于 2013-08-16T15:03:05.613 回答