2013-06-02 78 views
0
<code> 
     For Each oXElement In oXDocument.Descendants("searchResult") 
       sTitle = oXElement.Element("title").Value 
     Next 
</code> 

我也曾嘗試:如何解析XDocument(易趣)項目(列表)使用Visual Basic?

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

我有麻煩節點的保持,以及瞭解你的XDocument節點進行通信的方式。

我的最終目標是從所有易趣元素的屬性創建易趣對象模型。爲此,我需要以某種方式引用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> 

回答

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 
0

兩件事。首先,你落入陷阱,捕捉使用LINQ to XML問題的人中有90%的人。你忘了命名空間。您可以使用它在C#或VB工作如下:

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

VB也可以讓你使用進口的命名空間,就像你在你的文件的頂部導入其他.NET命名空間。此選項的優點是,如果您的項目中有模式,則可在構建查詢時通過XML結構獲得智能感知。

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

第二個問題是標題元素不是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