【发布时间】:2012-06-28 09:11:08
【问题描述】:
我已经坚持一个简单的 API 用法已经一个多星期了。以下是详细信息。
尝试对 ebay.com 进行 api 调用。 这是我的代码的样子...
这是起始页代码:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Results.aspx?Keywords=" + searchString.Text);
}
页面被定向到这段代码:
if (Request.QueryString["Keywords"] != null){
string keywords = Request.QueryString["Keywords"];
string myAppID = "HIDDEN";
var xml = XDocument.Load("http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=" + myAppID + "&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords=" + keywords + "&paginationInput.entriesPerPage=5");
XNamespace ns = "http://www.ebay.com/marketplace/search/v1/services";
var titles = from item in xml.Root.Descendants(ns + "title")
select new{
title = xml.Descendants(ns + "title").Select (x => x.Value),
};
foreach (var item in titles){
Label1.Text += item;
}
}
这是 xml 示例:
<findItemsByKeywordsResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
<searchReslut count="5">
<item>
<title></title>
</item>
<item>
<title></title>
</item>
<item>
<title></title>
</item>
我真的宁愿将项目变成一个数组,而不仅仅是列出它们。只是想我会先尝试更简单的方法。我得到的错误是: 我的标签的 for 循环输出如下所示:
{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }
并且输出异常是:
在 mscorlib.dll 中发生了“System.Threading.ThreadAbortException”类型的第一次机会异常 mscorlib.dll 中出现“System.Threading.ThreadAbortException”类型的异常,但未在用户代码中处理 线程 '' (0x27ee4) 已退出,代码为 0 (0x0)。
感谢任何帮助!
【问题讨论】:
-
样本中的 XML 太大了。我强烈建议将其缩小(大约 7-10 行),但仍然有效并重现问题。我怀疑您会在这样做时找到错误的来源。如果不是 - 会更容易回答。
-
以上编辑感谢 Alexei
标签: c# asp.net xml linq ebay-api