2012-06-23 30 views
0

我無法弄清楚如何解析具有多個元素的xml,並將它們寫出或分配給字符串數組,以便在我的web應用程序中使用它們。我總是被告知Ienumerable不能被轉換爲字符串。我真的只是試圖從XML中獲取每個項目,所以我可以使用它。用XML解析Ienumerable?

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我是:自IEnumerable

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> 

回答

1

返回值可以通過titles.ToArray()被轉換爲陣列(或列表),在此之後,可以Join串陣列的,象下面這樣:

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

此外,如果你喜歡所有這些連接沒有任何分隔符簡單地增加他們與一個簡單的for循環:

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

PS:我認爲你需要多個標籤,而不是一個標籤爲所有這些

+0

既不那些爲我工作的。第一個給我一個錯誤,說這個調用在這些屬性之間是有害的。第二種方法給我NullReferenceException未被用戶代碼處理。 – allencoded