自从我使用 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;