Vb.net 如何使用Visual Basic解析XDocument(易趣)项目(列表)?

Vb.net 如何使用Visual Basic解析XDocument(易趣)项目(列表)?,vb.net,parsing,linq-to-xml,Vb.net,Parsing,Linq To Xml,我也尝试过: <code> For Each oXElement In oXDocument.Descendants("searchResult") sTitle = oXElement.Element("title").Value Next </code> 我很难掌握节点以及理解与XDocument节点通信的方式 我的最终目标是从所有易趣元素的属性创建一个易趣对象模型。为此,我需要以某种方式引用XML标记—

我也尝试过:

<code> 
        For Each oXElement In oXDocument.Descendants("searchResult")
              sTitle = oXElement.Element("title").Value
        Next
</code>
我很难掌握节点以及理解与XDocument节点通信的方式

我的最终目标是从所有易趣元素的属性创建一个易趣对象模型。为此,我需要以某种方式引用XML标记——在这里,我希望您的建议或示例能够让我继续解析这个XML响应

非常感谢大家的帮助

PS:我搜索了一个类似的问题,找到了一些相同的问题,但仍然无法让我的解析工作

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

成功
1.12.0
2013-06-02T22:42:04.500Z
370821427802
现代数据库管理11E由Hoffer,Ramesh,Topi 11(国际版)
易趣美国
2228
教科书、教育
http://thumbs3.ebaystatic.com/m/meSAqCRbXecSjZjO1833dWQ/140.jpg
http://www.ebay.com/itm/Modern-Database-Management-11E-Hoffer-Ramesh-Topi-11th-Intl-Edition-/370821427802?pt=US_Texbook_Education
143649496
贝宝
真的
马来西亚
我的
0
自由的
在全世界
真的
假的
1.
54.07
54.07
活跃的
P20DT10H47M20S
假的
假的
2013-05-24T09:25:25.000Z
2013-06-23T09:29:24.000Z
库存
假的
真的
1000
崭新的
假的
真的
...
...
...
...
1.
5.
3.
14
http://www.ebay.com/ctg/143649496?LH_BIN=1&_ddo=1&_incaucbin=0&_ipg=5&_pgn=1

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

<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>
然后将其添加到每个
子体
元素
元素
属性
属性
,等等。您进行的调用:

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

两件事。首先,您落入了陷阱,90%的人在使用LINQ to XML时遇到了问题。您忘记了名称空间。您可以使用以下在C#或VB中工作的选项:

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

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

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
导入
第二个问题是title元素不是searchResult的直接子元素,而是嵌套在更深一层。下面是一个利用名称空间导入的示例。我使用VB XML文本作为后代(…)与任何给出C#偏颇答案的人形成对比;-)

公共类XmlTest
公共子TestXml()
Dim数据=
成功
1.12.0
2013-06-02T22:42:04.500Z
370821427802
现代数据库管理11E由Hoffer,Ramesh,Topi 11(国际版)
易趣美国
2228
教科书、教育
http://thumbs3.ebaystatic.com/m/meSAqCRbXecSjZjO1833dWQ/140.jpg
http://www.ebay.com/itm/Modern-Database-Management-11E-Hoffer-Ramesh-Topi-11th-Intl-Edition-/370821427802?pt=US_Texbook_Education
143649496
贝宝
真的
马来西亚
我的
0
自由的
在全世界
真的
假的
1.
54.07
54.07
活跃的
P20DT10H47M20S
假的
假的
2013-05-24T09:25:25.000Z
2013-06-23T09:29:24.000Z
库存
假的
真的
1000
崭新的
假的
真的
...
...
...
...
1.
5.
3.
14
对于数据中的每个el。。。
控制台写入线(el…值)
下一个
端接头
末级
Imports <xmlns:eb="http://www.ebay.com/marketplace/search/v1/services">
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