【问题标题】:Linq ebay XML Parsing ASP and C# using a query style?Linq ebay XML 使用查询样式解析 ASP 和 C#?
【发布时间】:2012-06-20 22:27:34
【问题描述】:

我已经有一段时间没有使用 ASP.Net 和 C#了。我正在尝试使用 C# 解析 XML API,我需要一些帮助。我的问题是我不太确定如何做到这一点。我也不断看到相互矛盾的方法。有些像我在下面做的那样。有些显示非常棒的查询,在我看来更好。

查询示例

IEnumerable<string> partNos =
    from item in purchaseOrder.Descendants("Item")
    select (string) item.Attribute("PartNumber");

哪种方法更好,我现在如何实现将 XML 解析为文本框?

这是 XML 格式: 此 XML 文件似乎没有任何与之关联的样式信息。文档树如下所示。

<findItemsByKeywordsResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
<ack>Success</ack>
<version>1.12.0</version>
<timestamp>2012-06-20T22:20:33.539Z</timestamp>
<searchResult count="1">
<item>
<itemId>390432965446</itemId>
<title>
Yamaha RX-V673 7.2 Channel 90 Watt Aventage Receiver {Brand New}
</title>
<globalId>EBAY-US</globalId>
<primaryCategory>
<categoryId>14981</categoryId>
<categoryName>Home Theater Receivers</categoryName>
</primaryCategory>
<galleryURL>
http://thumbs3.ebaystatic.com/pict/3904329654464040_1.jpg
</galleryURL>
<viewItemURL>
http://www.ebay.com/itm/Yamaha-RX-V673-7-2-Channel-90-Watt-Aventage-Receiver-Brand-New-/390432965446?pt=Receivers_Tuners
</viewItemURL>
<productId type="ReferenceID">114468754</productId>
<paymentMethod>PayPal</paymentMethod>
<autoPay>false</autoPay>
<postalCode>54143</postalCode>
<location>Marinette,WI,USA</location>
<country>US</country>
<shippingInfo>
<shippingServiceCost currencyId="USD">0.0</shippingServiceCost>
<shippingType>Free</shippingType>
<shipToLocations>US</shipToLocations>
<expeditedShipping>false</expeditedShipping>
<oneDayShippingAvailable>false</oneDayShippingAvailable>
<handlingTime>2</handlingTime>
</shippingInfo>
<sellingStatus>
<currentPrice currencyId="USD">519.0</currentPrice>
<convertedCurrentPrice currencyId="USD">519.0</convertedCurrentPrice>
<sellingState>Active</sellingState>
<timeLeft>P28DT23H32M35S</timeLeft>
</sellingStatus>
<listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>false</buyItNowAvailable>
<startTime>2012-06-19T21:48:08.000Z</startTime>
<endTime>2012-07-19T21:53:08.000Z</endTime>
<listingType>StoreInventory</listingType>
<gift>false</gift>
</listingInfo>
<returnsAccepted>true</returnsAccepted>
<condition>
<conditionId>1000</conditionId>
<conditionDisplayName>New</conditionDisplayName>
</condition>
<isMultiVariationListing>false</isMultiVariationListing>
</item>
</searchResult>
<paginationOutput>
<pageNumber>1</pageNumber>
<entriesPerPage>1</entriesPerPage>
<totalPages>1121495</totalPages>
<totalEntries>1121495</totalEntries>
</paginationOutput>
<itemSearchURL>
http://www.ebay.com/sch/i.html?_nkw=yamaha&_ddo=1&_ipg=1&_pgn=1
</itemSearchURL>
</findItemsByKeywordsResponse>

我的 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace ebayLinq
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string myAppID = "hidden from stack overflow";
            string ebayUrl = "http://svcs.ebay.com/services/search/FindingService/v1?";
            string operationName = "OPERATION-NAME=getSearchKeywordsRecommendation&";
            string serviceVersion = "SERVICE-VERSION=1.11.0&";
            string securityAppName = "SECURITY-APPNAME="+ myAppID +"&";
            string responseData = "RESPONSE-DATA-FORMAT=XML&";
            string rest = "REST-PAYLOAD&";

            string searchString = "macbook Pro";
            string keywords ="keywords="+searchString+"&";


            var xml = XDocument.Load(ebayUrl + 
                                        operationName + 
                                        serviceVersion + 
                                        securityAppName + 
                                        responseData);

            //XNamespace ns = "http://www.ebay.com/marketplace/search/v1/services";
            //XElement ack = xml.Root.Element(ns + "ack");    
        }
    }
}

好的,我可以让它与上面的代码一起工作,但到目前为止我不知道如何比 ack 更深入。我也宁愿做查询,而不是上面使用的方法。

任何输入的朋友?

根据您的意见,我想出了这个,但它不起作用?

   XElement convertedCurrentPrice = (from x in xml.Root.Descendants("title") select x).FirstOrDefault();
                string item = Convert.ToString(convertedCurrentPrice);
                TextBox1.Text = item;

【问题讨论】:

  • 那行不通。 convertCurrentPrice 是一个 XElement,据我所知,您不能只在元素本身上调用 .ToString 。试试这个:string item = convertCurrentPrice.Value; TextBox1.Text = item;

标签: c# asp.net linq xml-parsing


【解决方案1】:

我个人对解析 XML 的旧方法并不熟悉,但新的 LINQ to XML 是您正在谈论的查询样式,并且在从 XDocument 中提取信息时绝对可以让事情变得快速而简单。

如果您给我一个示例,说明您特别想从中提取数据的一个或多个节点,我可以帮助您解决这个问题,但基本结构是(例如,如果您想获取519.0)

XElement convertedCurrentPrice = (from x in xml.Root.Descendants("convertedCurrentPrice") select x).FirstOrDefault();

这将返回整个 XML 节点(和

那么获取值就这么简单:

double price = Convert.ToDecimal(convertedCurrentPrice.Value);

【讨论】:

  • 现在我想起来了,我很确定这也可以合并为一行。我会尽快回复你。
  • 如果您正在寻找特定的节点值,我可以帮助您处理语法。这是一个非常简单的 XDocument,因为似乎没有很多嵌套元素。另外,如果您觉得这有帮助,请随时点赞或标记为答案:)
  • 实际上我用你的代码尝试了一些东西,但它似乎不起作用。它运行,但 textbox1 内部没有显示任何内容。我将编辑我的回复并将我的代码发布在最底部。看看你是否意识到我不知道的事情。
【解决方案2】:

很难回答哪种语法更好。作为参考,它们被称为 Query SyntaxMethod Syntax。这是关于差异的MSDN article(由于可读性,本文推荐查询语法)。它们大致相当,我经常使用它们。

为了进一步参考,这里是一个讨论该主题的SO question

【讨论】:

  • 非常感谢。第一步清楚。现在我必须弄清楚如何使用查询语法
  • 如果您以方法语法发布查询,我可以帮助您将其翻译成查询语法以帮助您入门。其实很简单。
  • 非常感谢您的帮助!这对我来说是一场大风暴。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 2021-08-16
  • 1970-01-01
相关资源
最近更新 更多