0

我无法弄清楚如何解析具有多个同名元素的 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>
4

1 回答 1

1

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

label1.Text = String.Join(",", titles .ToArray());

此外,如果您想在没有任何分隔符的情况下连接所有这些,只需使用简单的 for 循环将它们相加:

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

PS:我认为您需要多个标签而不是所有标签

于 2012-06-23T20:33:37.793 回答