自從我使用ASP.Net和C#以來,這已經有一段時間了。我正在嘗試使用C#解析XML API,並且需要一些幫助。我的問題是我不太確定如何做到這一點。我仍然看到衝突的方法。有些人表現得像我在下面做的。有些顯示出令人敬畏的查詢,讓我看起來更好。查詢Linq ebay XML解析ASP和C#使用查詢樣式?
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;
那不是要去工作CON vertedCurrentPrice是一個XElement,就我所知,你不能只在元素本身上調用.ToString。試試這個: string item = convertedCurrentPrice.Value; TextBox1.Text = item; –