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

时间:2022-11-11 23:44:17
<code> 
        For Each oXElement In oXDocument.Descendants("searchResult")
              sTitle = oXElement.Element("title").Value
        Next
</code>

I have also tried:

我也尝试过:

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

I am having trouble getting a hold of nodes as well as understanding the way you communicate with XDocument nodes.

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

My Ultimate goal is to create an Ebay Object Model From all Ebay Element's Attributes. For that I need to refer to XML tag somehow - and this is where I would appreciate your advice or sample example that could let me proceed with parsing out this XML response.

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

Thank you all much for any help.

非常感谢你的帮助。

PS: I have searched for a similar questions and found a few of the same kind but still could not get my parsing to work.

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 个解决方案

#1


0  

You have to use XNamespace instance when querying your XML:

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

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

And with that add it to every Descendants, Elements, Element, Attributes, Attributes, etc. calls you make:

然后将它添加到您制作的每个后代,元素,元素,属性,属性等调用中:

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

#2


0  

Two things. First, you fell into the trap that catches 90% of the people with problems using LINQ to XML. You forgot the namespace. You can use the following which works in C# or VB:

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

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

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

VB Also lets you use a Imports for a namespace just as you import other .Net namespaces at the top of your file. The advantage of this option is that if you have a schema in your project, you get intellisense over the XML structure while building your query.

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

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

The second issue you have is that the title element is not a direct child of searchResult, but is nested an additional level deeper. Here's a sample leveraging the imports for the namespace. I'm using the VB XML Literals for descendents (...) for contrast with anyone giving you a C# biased answer ;-)

你遇到的第二个问题是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

#1


0  

You have to use XNamespace instance when querying your XML:

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

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

And with that add it to every Descendants, Elements, Element, Attributes, Attributes, etc. calls you make:

然后将它添加到您制作的每个后代,元素,元素,属性,属性等调用中:

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

#2


0  

Two things. First, you fell into the trap that catches 90% of the people with problems using LINQ to XML. You forgot the namespace. You can use the following which works in C# or VB:

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

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

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

VB Also lets you use a Imports for a namespace just as you import other .Net namespaces at the top of your file. The advantage of this option is that if you have a schema in your project, you get intellisense over the XML structure while building your query.

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

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

The second issue you have is that the title element is not a direct child of searchResult, but is nested an additional level deeper. Here's a sample leveraging the imports for the namespace. I'm using the VB XML Literals for descendents (...) for contrast with anyone giving you a C# biased answer ;-)

你遇到的第二个问题是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