ASP.NET C#System.Linq.Enumerable + WhereSelectEnumerable错误

时间:2012-06-27 15:33:39

标签: c# asp.net xml linq

得到这个奇怪的LINQ错误。

title = System.Linq.Enumerable + WhereSelectEnumerableIterator`2 [System.Xml.Linq.XElement,System.String

这是我的代码:

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>

尝试让它正确输出。

2 个答案:

答案 0 :(得分:3)

而不是

title = xml.Descendants(ns + "title").Select (x => x.Value)

更改为

title = item.Value

编辑ChrisGessler建议,但我的建议是:

if (Request.QueryString["Keywords"] != null)
{
    string keywords = Request.QueryString["Keywords"];
    string myAppID = "HIDDEN";
    var xml = XDocument.Load(/* snip */);
    XNamespace ns = "http://www.ebay.com/marketplace/search/v1/services";
    var titles = xml.Root.Descendants(ns + "title").Select(x => x.Value);
    Label1.Text = String.Join(null, titles);
}

答案 1 :(得分:1)

我在想这个:

var titles = from item in xml.Root.Descendants(ns + "title")                               
             select new{                                   
                title = xml.Descendants(ns + "title").Select (x => x.Value)}; 

应该是:

var titles = from item in xml.Root.Descendants(ns + "title")                               
             select item.Value);