【问题标题】:Can't retrieve nested information from eBay API无法从 eBay API 检索嵌套信息
【发布时间】:2014-09-26 21:58:26
【问题描述】:

我正在使用带有 eBay API 的 Linq to XML,甚至无法从返回的 XML 中检索基本信息。我已经尝试了from x in y select z 等的所有组合,但没有运气。

我正在加载数据

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->Item 中的信息。

所以上面我的意思是元素的值......

findItemsByKeywordsResponse->ack findItemsByKeywordsResponse->版本

还有嵌套信息

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

我花了几天时间在网站上寻找答案,但没有找到可行的解决方案。

【问题讨论】:

    标签: c# xml linq ebay-api


    【解决方案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);
    

    【讨论】:

    • 也非常感谢,非常感谢。
    【解决方案2】:

    最可能的问题是命名空间。文档中的元素位于命名空间 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
    

    【讨论】:

    • 非常感谢,这就是问题所在。
    【解决方案3】:

    Ebay 本身提供了一个类库,可以对 ebay 服务器的 clls 进行包装。通过它你可以直接接收强类型对象,而不必自己解析返回的xml。

    看看这里,可能会有所帮助:

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

    在“工具”下,您可以找到下载内容,包括适用于 .NET 和 Java 的库和示例项目。

    【讨论】:

    • 谢谢,我从这里开始,但这对我来说更像是一个学习曲线,我很想学习一种 LINQ 方法,但非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多