Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用XML解析Ienumerable?_C#_Xml_Linq - Fatal编程技术网

C# 用XML解析Ienumerable?

C# 用XML解析Ienumerable?,c#,xml,linq,C#,Xml,Linq,我不知道如何解析包含多个同名元素的xml,并将它们写出或分配给字符串数组,以便在web应用程序中使用它们。我一直被告知Ienumerable无法转换为字符串。我真的试图简单地从XML中提取每一项,以便使用它 if (Request.QueryString["Keywords"] != null) { string keywords = Request.QueryString["Keywords"]; string myAppID =

我不知道如何解析包含多个同名元素的xml,并将它们写出或分配给字符串数组,以便在web应用程序中使用它们。我一直被告知Ienumerable无法转换为字符串。我真的试图简单地从XML中提取每一项,以便使用它

if (Request.QueryString["Keywords"] != null)
        {
            string keywords = Request.QueryString["Keywords"];
            string myAppID = "HIDDEN FROM STACKOVERFLOW";
            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=10");
            XNamespace ns = "http://www.ebay.com/marketplace/search/v1/services";

 var titles = from item in xml.Root.Descendants(ns + "title")
                              select new
                              {
                                  title = item.Parent.Element("title").Value,
                              };
            Label1.Text = titles.ToString();
        }
我的示例XML是:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<findItemsByKeywordsResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
<ack>Success</ack>
<version>1.12.0</version>
<timestamp>2012-06-23T19:54:46.019Z</timestamp>
<searchResult count="10">
<item>
<itemId>120933737016</itemId>
<title>
Apple MacBook Pro 13.3" Laptop - MC700LL/A (February, 2011) 8GB 320GB *GEM MINT*
</title>
<globalId>EBAY-US</globalId>
<subtitle>
* GEM MINT, LION, 8GB RAM, ALWAYS stored in Speck Case
</subtitle>
<primaryCategory>
<categoryId>111422</categoryId>
<categoryName>Apple Laptops</categoryName>
</primaryCategory>
<galleryURL>
http://thumbs1.ebaystatic.com/pict/1209337370164040_1.jpg
</galleryURL>
<viewItemURL>
http://www.ebay.com/itm/Apple-MacBook-Pro-13-3-Laptop-MC700LL-A-February-2011-8GB-320GB-GEM-MINT-/120933737016?pt=Apple_Laptops
</viewItemURL>
<productId type="ReferenceID">99741550</productId>
<paymentMethod>PayPal</paymentMethod>
<autoPay>false</autoPay>
<postalCode>98296</postalCode>
<location>Snohomish,WA,USA</location>
<country>US</country>
<shippingInfo>
<shippingServiceCost currencyId="USD">17.0</shippingServiceCost>
<shippingType>Flat</shippingType>
<shipToLocations>US</shipToLocations>
<expeditedShipping>false</expeditedShipping>
<oneDayShippingAvailable>false</oneDayShippingAvailable>
<handlingTime>1</handlingTime>
</shippingInfo>
<sellingStatus>
<currentPrice currencyId="USD">811.99</currentPrice>
<convertedCurrentPrice currencyId="USD">811.99</convertedCurrentPrice>
<bidCount>14</bidCount>
<sellingState>Active</sellingState>
<timeLeft>P0DT0H5M24S</timeLeft>
</sellingStatus>
<listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>false</buyItNowAvailable>
<startTime>2012-06-16T20:00:10.000Z</startTime>
<endTime>2012-06-23T20:00:10.000Z</endTime>
<listingType>Auction</listingType>
<gift>false</gift>
</listingInfo>
<returnsAccepted>false</returnsAccepted>
<galleryPlusPictureURL>
http://galleryplus.ebayimg.com/ws/web/120933737016_1_0_1.jpg
</galleryPlusPictureURL>
<condition>
<conditionId>3000</conditionId>
<conditionDisplayName>Used</conditionDisplayName>
</condition>
<isMultiVariationListing>false</isMultiVariationListing>
</item>
<item>
<itemId>261037967463</itemId>
<title>
2009 Apple MACBOOK 2.13GHz Core 2 Duo / 160GB HDD / 2GB RAM / 1 YEAR WARRANTY!
</title>
<globalId>EBAY-US</globalId>
<primaryCategory>
<categoryId>111422</categoryId>
<categoryName>Apple Laptops</categoryName>
</primaryCategory>
<galleryURL>
http://thumbs4.ebaystatic.com/pict/2610379674634040_1.jpg
</galleryURL>
<viewItemURL>
http://www.ebay.com/itm/2009-Apple-MACBOOK-2-13GHz-Core-2-Duo-160GB-HDD-2GB-RAM-1-YEAR-WARRANTY-/261037967463?pt=Apple_Laptops
</viewItemURL>
<productId type="ReferenceID">85150888</productId>
<paymentMethod>PayPal</paymentMethod>
<autoPay>true</autoPay>
<postalCode>92101</postalCode>
<location>San Diego,CA,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>1</handlingTime>
</shippingInfo>
<sellingStatus>
<currentPrice currencyId="USD">498.0</currentPrice>
<convertedCurrentPrice currencyId="USD">498.0</convertedCurrentPrice>
<sellingState>Active</sellingState>
<timeLeft>P10DT6H30M15S</timeLeft>
</sellingStatus>
<listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>false</buyItNowAvailable>
<startTime>2012-06-04T02:20:01.000Z</startTime>
<endTime>2012-07-04T02:25:01.000Z</endTime>
<listingType>StoreInventory</listingType>
<gift>false</gift>
</listingInfo>
<returnsAccepted>true</returnsAccepted>
<condition>
<conditionId>2500</conditionId>
<conditionDisplayName>Seller refurbished</conditionDisplayName>
</condition>
<isMultiVariationListing>false</isMultiVariationListing>
</item>
<item>
<itemId>271003897097</itemId>
<title>
Apple MacBook Pro 15.4" Laptop (February, 2011) - Customized
</title>
<globalId>EBAY-US</globalId>
<subtitle>Apple Care till June 2014 - Like NEW!</subtitle>
<primaryCategory>
<categoryId>111422</categoryId>
<categoryName>Apple Laptops</categoryName>
</primaryCategory>
<galleryURL>
http://thumbs2.ebaystatic.com/pict/2710038970974040_1.jpg
</galleryURL>
<viewItemURL>
http://www.ebay.com/itm/Apple-MacBook-Pro-15-4-Laptop-February-2011-Customized-/271003897097?pt=Apple_Laptops
</viewItemURL>
<productId type="ReferenceID">108789851</productId>
<paymentMethod>PayPal</paymentMethod>
<autoPay>true</autoPay>
<postalCode>94536</postalCode>
<location>Fremont,CA,USA</location>
<country>US</country>
<shippingInfo>
<shippingServiceCost currencyId="USD">35.0</shippingServiceCost>
<shippingType>Flat</shippingType>
<shipToLocations>US</shipToLocations>
<expeditedShipping>true</expeditedShipping>
<oneDayShippingAvailable>false</oneDayShippingAvailable>
<handlingTime>2</handlingTime>
</shippingInfo>
<sellingStatus>
<currentPrice currencyId="USD">1399.99</currentPrice>
<convertedCurrentPrice currencyId="USD">1399.99</convertedCurrentPrice>
<bidCount>0</bidCount>
<sellingState>Active</sellingState>
<timeLeft>P0DT0H10M50S</timeLeft>
</sellingStatus>
<listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>true</buyItNowAvailable>
<buyItNowPrice currencyId="USD">1750.0</buyItNowPrice>
<convertedBuyItNowPrice currencyId="USD">1750.0</convertedBuyItNowPrice>
<startTime>2012-06-22T20:05:36.000Z</startTime>
<endTime>2012-06-23T20:05:36.000Z</endTime>
<listingType>AuctionWithBIN</listingType>
<gift>false</gift>
</listingInfo>
<returnsAccepted>false</returnsAccepted>
<galleryPlusPictureURL>
http://galleryplus.ebayimg.com/ws/web/271003897097_1_0_1.jpg
</galleryPlusPictureURL>
<condition>
<conditionId>3000</conditionId>
<conditionDisplayName>Used</conditionDisplayName>
</condition>
<isMultiVariationListing>false</isMultiVariationListing>
</item>
<item>
<itemId>221050903140</itemId>
<title>Apple MacBook 13.3" Laptop - MB062LL/A (2006)</title>
<globalId>EBAY-US</globalId>
<primaryCategory>
<categoryId>111422</categoryId>
<categoryName>Apple Laptops</categoryName>
</primaryCategory>
<galleryURL>
http://thumbs1.ebaystatic.com/pict/2210509031404040_1.jpg
</galleryURL>
<viewItemURL>
http://www.ebay.com/itm/Apple-MacBook-13-3-Laptop-MB062LL-A-2006-/221050903140?pt=Apple_Laptops
</viewItemURL>
<productId type="ReferenceID">78092354</productId>
<paymentMethod>PayPal</paymentMethod>
<autoPay>false</autoPay>
<postalCode>80524</postalCode>
<location>Fort Collins,CO,USA</location>
<country>US</country>
<shippingInfo>
<shippingServiceCost currencyId="USD">20.0</shippingServiceCost>
<shippingType>Flat</shippingType>
<shipToLocations>US</shipToLocations>
<expeditedShipping>true</expeditedShipping>
<oneDayShippingAvailable>false</oneDayShippingAvailable>
<handlingTime>3</handlingTime>
</shippingInfo>
<sellingStatus>
<currentPrice currencyId="USD">250.0</currentPrice>
<convertedCurrentPrice currencyId="USD">250.0</convertedCurrentPrice>
<bidCount>0</bidCount>
<sellingState>Active</sellingState>
<timeLeft>P0DT0H13M21S</timeLeft>
</sellingStatus>
<listingInfo>
<bestOfferEnabled>false</bestOfferEnabled>
<buyItNowAvailable>false</buyItNowAvailable>
<startTime>2012-06-18T20:08:07.000Z</startTime>
<endTime>2012-06-23T20:08:07.000Z</endTime>
<listingType>Auction</listingType>
<gift>false</gift>
</listingInfo>
<returnsAccepted>false</returnsAccepted>
<condition>
<conditionId>3000</conditionId>
<conditionDisplayName>Used</conditionDisplayName>
</condition>
<isMultiVariationListing>false</isMultiVariationListing>
</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
</searchResult>
<paginationOutput>
<pageNumber>1</pageNumber>
<entriesPerPage>10</entriesPerPage>
<totalPages>11900</totalPages>
<totalEntries>118994</totalEntries>
</paginationOutput>
<itemSearchURL>
http://www.ebay.com/sch/i.html?_nkw=%22macbook%22&_ddo=1&_ipg=10&_pgn=1
</itemSearchURL>
</findItemsByKeywordsResponse>
此XML文件似乎没有任何与之关联的样式信息。文档树如下所示。
成功
1.12.0
2012-06-23T19:54:46.019Z
120933737016
苹果MacBook Pro 13.3英寸笔记本电脑-MC700LL/A(2011年2月)8GB 320GB*GEM MINT*
易趣美国
*宝石造币厂,狮子,8GB内存,始终存储在斑点盒中
111422
苹果笔记本电脑
http://thumbs1.ebaystatic.com/pict/1209337370164040_1.jpg
http://www.ebay.com/itm/Apple-MacBook-Pro-13-3-Laptop-MC700LL-A-February-2011-8GB-320GB-GEM-MINT-/120933737016?pt=Apple_Laptops
99741550
贝宝
假的
98296
斯诺霍米什,华盛顿州,美国
美国
17
平的
美国
假的
假的
1.
811.99
811.99
14
活跃的
P0DT0H5M24S
假的
假的
2012-06-16T20:00:10.000Z
2012-06-23T20:00:10.000Z
拍卖
假的
假的
http://galleryplus.ebayimg.com/ws/web/120933737016_1_0_1.jpg
3000
使用
假的
261037967463
2009年苹果MACBOOK 2.13GHz Core 2 Duo/160GB HDD/2GB RAM/一年保修!
易趣美国
111422
苹果笔记本电脑
http://thumbs4.ebaystatic.com/pict/2610379674634040_1.jpg
http://www.ebay.com/itm/2009-Apple-MACBOOK-2-13GHz-Core-2-Duo-160GB-HDD-2GB-RAM-1-YEAR-WARRANTY-/261037967463?pt=Apple_Laptops
85150888
贝宝
真的
92101
美国加利福尼亚州圣地亚哥
美国
0
自由的
美国
假的
假的
1.
498
498
活跃的
P10DT6H30M15S
假的
假的
2012-06-04T02:20:01.000Z
2012-07-04T02:25:01.000Z
库存
假的
真的
2500
卖方翻新
假的
271003897097
苹果MacBookPro 15.4英寸笔记本电脑(2011年2月)-定制
易趣美国
2014年6月前的苹果护理-如新!
111422
苹果笔记本电脑
http://thumbs2.ebaystatic.com/pict/2710038970974040_1.jpg
http://www.ebay.com/itm/Apple-MacBook-Pro-15-4-Laptop-February-2011-Customized-/271003897097?pt=Apple_Laptops
108789851
贝宝
真的
94536
美国加利福尼亚州弗里蒙特
美国
35
平的
美国
真的
假的
2.
1399.99
1399.99
0
活跃的
P0DT0H10M50S
假的
真的
1750
1750
2012-06-22T20:05:36.000Z
2012-06-23T20:05:36.000Z
拍卖行
假的
假的
http://galleryplus.ebayimg.com/ws/web/271003897097_1_0_1.jpg
3000
使用
假的
221050903140
苹果MacBook 13.3英寸笔记本电脑-MB062LL/A(2006年)
易趣美国
111422
苹果笔记本电脑
http://thumbs1.ebaystatic.com/pict/2210509031404040_1.jpg
http://www.ebay.com/itm/Apple-MacBook-13-3-Laptop-MB062LL-A-2006-/221050903140?pt=Apple_Laptops
78092354
贝宝
假的
80524
美国柯林斯堡
美国
20
平的
美国
真的
假的
3.
250
250
0
活跃的
P0DT0H13M21S
假的
假的
2012-06-18T20:08:07.000Z
2012-06-23T20:08:07.000Z
拍卖
假的
假的
3000
使用
假的
...
...
...
...
...
...
1.
10
11900
118994
http://www.ebay.com/sch/i.html?_nkw=%22macbook%22&_ddo=1&_ipg=10&_pgn=1

可以通过
titles.ToArray()
将IEnumerable的返回值转换为数组(或列表),然后可以转换数组字符串,如下所示:

label1.Text = String.Join(",", titles .ToArray());
此外,如果您希望在不使用任何分隔符的情况下连接它们,只需使用简单的for循环将它们相加:

foreach(var item in titles)
{
    label1.Text += item;
} 

PS:我认为您需要多个标签,而不是一个标签,所有标签都不适用于我。第一个给我一个错误,说调用在属性之间不明确。第二个方法给我NullReferenceException,用户代码未处理异常。