2010-11-17 112 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样本混乱。我想这就是你学习的方式。 :)

感谢球员:)