2010-11-17 111 views
0

嗨,我剛剛在幾天前着手LINQ-XML,想知道我是否做錯了什麼,或者只是不可能做到這一點。我四處搜尋,沒有發現任何與我有關的問題,現在我一直在瞎搞。用linq選擇子元素

XML:

<catalog> 
<product description="Cardigan Sweater" product_image="cardigan.jpg"> 
<catalog_item gender="Men's"> 
    <item_number>QWZ5671</item_number> 
    <price>39.95</price> 
    <size description="Medium"> 
    <color_swatch image="red_cardigan.jpg">Red</color_swatch> 
    <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
    </size> 
    <size description="Large"> 
    <color_swatch image="red_cardigan.jpg">Red</color_swatch> 
    <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
    </size> 
</catalog_item> 
<catalog_item gender="Women's"> 
    <item_number>RRX9856</item_number> 
    <price>42.50</price> 
    <size description="Small"> 
    <color_swatch image="red_cardigan.jpg">Red</color_swatch> 
    <color_swatch image="navy_cardigan.jpg">Navy</color_swatch> 
    <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
    </size> 
    <size description="Medium"> 
    <color_swatch image="red_cardigan.jpg">Red</color_swatch> 
    <color_swatch image="navy_cardigan.jpg">Navy</color_swatch> 
    <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
    <color_swatch image="black_cardigan.jpg">Black</color_swatch> 
    </size> 
    <size description="Large"> 
    <color_swatch image="navy_cardigan.jpg">Navy</color_swatch> 
    <color_swatch image="black_cardigan.jpg">Black</color_swatch> 
    </size> 
    <size description="Extra Large"> 
    <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
    <color_swatch image="black_cardigan.jpg">Black</color_swatch> 
    </size> 
</catalog_item> 

和查詢:

 var query = 
     from size in 
      (
       from catalogItem in cardigan.Descendants("catalog_item") 
       where catalogItem.Attribute("gender").Value == "Men's" 
       select catalogItem.Descendants("size") 
      ) 
     select size.Elements("color_swatch"); 

,基本上讓我全部爲男性的color_swatch,但我一直在努力,看看我是否能得到所有color_swatch男士大號只有。

在此先感謝。

回答

0

只要改變你的查詢是

var query = 
    from size in 
     (
      from catalogItem in cardigan.Descendants("catalog_item") 
      where catalogItem.Attribute("gender").Value == "Men's" 
      select catalogItem.Descendants("size") 
     ) 
    where size.Attribute("description") == "Large" 
    select size.Elements("color_swatch"); 

這工作,因爲你第一次得到適用男性所有的「大小」項,然後從列表它過濾那些只抓「大」的。

+0

您的意思是'size.Attribute',而不是'size.Attributes'。 – cdhowie 2010-11-17 22:50:15

+0

謝謝,修正了輸入錯誤 – 2010-11-17 22:50:45

+0

看到的問題是,尺寸是IEnumerable 不是XElement,所以它沒有Attribute的定義。我已經試過這:( – David 2010-11-17 22:53:54

0

行,所以我搞砸周圍的一些更...我知道了......

var query =   
from catalogItem in cardigan.Descendants("catalog_item") 
where catalogItem.Attribute("gender").Value == "Men's" 
from size in catalogItem.Descendants("size") 
where size.Attribute("description").Value == "Large" 
select size.Elements("color_swatch"); 

花費約8小時,但我明白了!

我去了,基本上得到了一些複雜的XML樣本混亂。我想這就是你學習的方式。 :)

感謝球員:)