将XML中的ebay webservice响应转换为C#对象?

时间:2013-03-10 16:23:15

标签: c# xml windows-phone-7 parsing deserialization

我正在创建一个Windows Phone 7应用程序,其中我正在使用Ebay链接

http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=AppID&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords=harry%20potter%20phoenix

以下列格式获取项目列表

<findItemsByKeywordsResponse>
  <ack>Success</ack>
  <version>1.12.0</version>
  <timestamp>2013-03-10T15:50:40.717Z</timestamp>
  <searchResult count="100">
    <item></item>
    <item></item>
    <item>
      <itemId>221170783013</itemId>
      <title>Harry Potter and the Order of the Phoenix (DVD, 2007, Full Frame) - New Unopened</title>
      <globalId>EBAY-ENCA</globalId>
      <primaryCategory>
        <categoryId>617</categoryId>
        <categoryName>DVDs & Blu-ray Discs</categoryName>
      </primaryCategory>
      <galleryURL>http://thumbs2.ebaystatic.com/m/mm6I9OfrWGLKdYk-gLVSacg/140.jpg</galleryURL>
      <viewItemURL>http://www.ebay.com/itm/Harry-Potter-and-Order-Phoenix-DVD-2007-Full-Frame-New-Unopened-/221170783013?pt=US_DVD_HD_DVD_Blu_ray</viewItemURL>
      <productId type="ReferenceID">62940319</productId>
      <paymentMethod>PayPal</paymentMethod>
      <autoPay>false</autoPay>
      <postalCode>P3P1S9</postalCode>
      <location>Canada</location>
      <country>CA</country>
      <shippingInfo>
        <shippingServiceCost currencyId="USD">4.99</shippingServiceCost>
        <shippingType>Flat</shippingType>
        <shipToLocations>Worldwide</shipToLocations>
        <expeditedShipping>false</expeditedShipping>
        <oneDayShippingAvailable>false</oneDayShippingAvailable>
        <handlingTime>1</handlingTime>
      </shippingInfo>
      <sellingStatus></sellingStatus>
      <listingInfo></listingInfo>
      <returnsAccepted>true</returnsAccepted>
      <condition></condition>
      <isMultiVariationListing>false</isMultiVariationListing>
      <topRatedListing>false</topRatedListing>
    </item>
    <item>
      <itemId>130861888976</itemId>
      <title>Harry Potter and the Order of the Phoenix Year 5 by J. K. Rowling (2003,...</title>
      <globalId>EBAY-US</globalId>
      <primaryCategory>
        <categoryId>279</categoryId>
        <categoryName>Children & Young Adults</categoryName>
      </primaryCategory>
      <galleryURL>http://thumbs1.ebaystatic.com/m/mkjj5zzPkMQ6FRLW5FlvXZQ/140.jpg</galleryURL>
      <viewItemURL>http://www.ebay.com/itm/Harry-Potter-and-Order-Phoenix-Year-5-J-K-Rowling-2003-/130861888976?pt=US_Childrens_Books</viewItemURL>
      <productId type="ReferenceID">2327833</productId>
      <paymentMethod>PayPal</paymentMethod>
      <autoPay>false</autoPay>
      <postalCode>30535</postalCode>
      <location>Demorest,GA,USA</location>
      <country>US</country>
      <shippingInfo>
        <shippingServiceCost currencyId="USD">4.0</shippingServiceCost>
        <shippingType>Flat</shippingType>
        <shipToLocations>US</shipToLocations>
        <expeditedShipping>false</expeditedShipping>
        <oneDayShippingAvailable>false</oneDayShippingAvailable>
        <handlingTime>3</handlingTime>
      </shippingInfo>
      <sellingStatus>
        <currentPrice currencyId="USD">12.99</currentPrice>
        <convertedCurrentPrice currencyId="USD">12.99</convertedCurrentPrice>
        <bidCount>0</bidCount>
        <sellingState>Active</sellingState>
        <timeLeft>P0DT0H21M54S</timeLeft>
      </sellingStatus>
      <listingInfo>
        <bestOfferEnabled>false</bestOfferEnabled>
        <buyItNowAvailable>false</buyItNowAvailable>
        <startTime>2013-03-03T16:12:34.000Z</startTime>
        <endTime>2013-03-10T16:12:34.000Z</endTime>
        <listingType>Auction</listingType>
        <gift>false</gift>
      </listingInfo>
      <returnsAccepted>false</returnsAccepted>
      <condition>
        <conditionId>1000</conditionId>
        <conditionDisplayName>Brand New</conditionDisplayName>
      </condition>
      <isMultiVariationListing>false</isMultiVariationListing>
      <topRatedListing>false</topRatedListing>
    </item>
  </searchResult>
  <paginationOutput>
    <totalPages>27</totalPages>
    <totalEntries>2602</totalEntries>
    <pageNumber>1</pageNumber>
    <entriesPerPage>100</entriesPerPage>
  </paginationOutput>
  <itemSearchURL>http://www.ebay.com/sch/i.html?_nkw=harry+potter+phoenix&_ddo=1&_ipg=100&_pgn=1</itemSearchURL>
</findItemsByKeywordsResponse>

在整个响应中我需要列表或包含Title,Description,EndTime,ListType,currentprice的项目的ObservableCollections。

对此进行编码的最佳方法是什么。我无法想到最简单的思考方式,就像直接反序列化对C#对象的XML响应一样。

我还没有尝试过任何代码但我目前很困惑如何实现这个目标?

2 个答案:

答案 0 :(得分:3)

最优化的?编写一个基于XmlReader的解析器。一种相当快速的无头痛方式?使用XLinq。未测试的:

var items = XDocument.Parse(data)
    .Elements("findItemsByKeywordsResponse")
    .Elements("searchResult")
    .Elements("item")
    .Select(item =>
    {
        var li = item.Element("listingInfo");

        return new
        {
            Title = (string)item.Element("title"),
            EndTime = li != null ? (DateTime?)li.Element("endTime") : null,
            ListingType = li != null ? (string)li.Element("listingType") : null,
        };
    });

答案 1 :(得分:2)

我建议您使用JSON版本而不是XML,以便解析更容易。

将RESPONSE-DATA-FORMAT更改为JSON,然后按照THIS BLOG POST中提到的过程进行操作,这有助于您解析JSON数据。如果您仍然遇到任何问题,请再次询问。