2011-09-04 18 views
0

我正在使用Linq將XML與eBay API配合使用,並且無法從返回的XML中檢索基本信息。我已經嘗試過from x in y select z等所有組合,但沒有運氣。無法從eBay API檢索嵌套信息

我加載與

var xml = XDocument.Load ("http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=***MY-KEY-OBSCURED**&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords=yamaha&paginationInput.entriesPerPage=1&paginationInput.pageNumber=1"); 

的數據,並根據我的控制檯和LINQPad得到以下XML。

<findItemsByKeywordsResponse xmlns="http://www.ebay.com/marketplace/search/v1/services"> <ack>Success</ack> <version>1.11.0</version> <timestamp>2011-09-04T12:15:10.595Z</timestamp> <searchResult count="1"> 
    <item> 
     <itemId>220841819907</itemId> 
     <title>YAMAHA RX-V592 SURROUND SOUND RECEIVER</title> 
     <globalId>EBAY-US</globalId> 
     <primaryCategory> 
     <categoryId>14981</categoryId> 
     <categoryName>Receivers</categoryName> 
     </primaryCategory> 
     <galleryURL>http://thumbs4.ebaystatic.com/pict/2208418199074040_1.jpg</galleryURL> 
     <viewItemURL>http://www.ebay.com/itm/YAMAHA-RX-V592-SURROUND-SOUND-RECEIVER-/220841819907?pt=Receivers_Tuners</viewItemURL> 
     <productId type="ReferenceID">46568009</productId> 
     <paymentMethod>PayPal</paymentMethod> 
     <autoPay>false</autoPay> 
     <postalCode>76638</postalCode> 
     <location>Crawford,TX,USA</location> 
     <country>US</country> 
     <shippingInfo> 
     <shippingServiceCost currencyId="USD">22.0</shippingServiceCost> 
     <shippingType>Flat</shippingType> 
     <expeditedShipping>false</expeditedShipping> 
     <oneDayShippingAvailable>false</oneDayShippingAvailable> 
     <handlingTime>3</handlingTime> 
     <shipToLocations>US</shipToLocations> 
     </shippingInfo> 
     <sellingStatus> 
     <currentPrice currencyId="USD">51.0</currentPrice> 
     <convertedCurrentPrice currencyId="USD">51.0</convertedCurrentPrice> 
     <bidCount>13</bidCount> 
     <sellingState>Active</sellingState> 
     <timeLeft>P0DT0H18M17S</timeLeft> 
     </sellingStatus> 
     <listingInfo> 
     <bestOfferEnabled>false</bestOfferEnabled> 
     <buyItNowAvailable>false</buyItNowAvailable> 
     <startTime>2011-08-28T12:33:27.000Z</startTime> 
     <endTime>2011-09-04T12:33:27.000Z</endTime> 
     <listingType>Auction</listingType> 
     <gift>false</gift> 
     </listingInfo> 
     <returnsAccepted>false</returnsAccepted> 
     <condition> 
     <conditionId>3000</conditionId> 
     <conditionDisplayName>Used</conditionDisplayName> 
     </condition> 
     <isMultiVariationListing>false</isMultiVariationListing> 
    </item> </searchResult> <paginationOutput> 
    <pageNumber>1</pageNumber> 
    <entriesPerPage>1</entriesPerPage> 
    <totalPages>819204</totalPages> 
    <totalEntries>819204</totalEntries> </paginationOutput> <itemSearchURL>http://www.ebay.com/sch/i.html?_nkw=yamaha&amp;_ddo=1&amp;_ipg=1&amp;_pgn=1</itemSearchURL> </findItemsByKeywordsResponse> 

誰能幫我找到一線信息,如ACK和版本,然後再嵌套searchResult->項目內的信息。

因此,通過以上我指的是元素的值...

 
findItemsByKeywordsResponse->ack 
findItemsByKeywordsResponse->version 

,並嵌套信息

 
findItemsByKeywordsResponse->searchResult->item->itemId 
findItemsByKeywordsResponse->searchResult->item->title 

我花了幾天拖網答案的網站,但沒有發現任何工作方案。

回答

1

您沒有顯示任何您嘗試過的代碼,但我強烈懷疑您只是缺少名稱空間。這樣的代碼:

XNamespace ns = "http://www.ebay.com/marketplace/search/v1/services" 

XElement ack = doc.Root.Element(ns + "ack"); 
XElement version = doc.Root.Element(ns + "version"); 
IEnumerable<string> itemIds = doc.Root.Elements(ns + "searchResult") 
             .Element(ns + "item") 
             .Element(ns + "itemId") 
             .Select(x => (string) x); 
+0

非常感謝,非常感謝。 – GHarping

0

易趣本身提供了一個類庫來包裝周圍的易趣到易趣服務器。通過它您可以直接接收強類型對象,而不必自己分析返回的xml。

看看這裏,可能會有幫助:

http://developer.ebay.com/products/finding/

下的「工具」,你可以找到下載包括圖書館和.NET和Java樣本項目。

+0

謝謝,我開始在這裏然而,這是更適合我一個學習曲線,並很希望學習LINQ的方法,但很感謝您的幫助。 – GHarping

1

最有可能的問題是命名空間。文檔中的元素位於命名空間http://www.ebay.com/marketplace/search/v1/services中,您必須在查詢中反映這一點。所以,用這樣的:

XNamespace ns = "http://www.ebay.com/marketplace/search/v1/services"; 

檢索出的ack值:

xml.Root.Element(ns + "ack").Value 
+0

非常感謝,這是問題所在。 – GHarping