开发者

selecting child child elements with linq

开发者 https://www.devze.com 2023-01-25 20:45 出处:网络
Hi I just dove into LINQ-XML a few days ago and would like to know if i\'m doing something wrong or it\'s just not possible to do this. I\'ve searched around and there hasn\'t been any problem related

Hi I just dove into LINQ-XML a few days ago and would like to know if i'm doing something wrong or it's just not possible to do this. I've searched around and there hasn't been any problem related to mine and I've been mucking around a bit now.

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>

and the Query:

        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");

that basically gets me all the color_swatch for Men's but I've been trying to see if I can get all the color_swatch for开发者_StackOverflow中文版 Men's Large Only.

Thanks in advance.


Simply change your query to be

    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");

This will work because you first get all "Size" items that apply for Men, then from that list it filters those only grabbing the "Large" ones.


ok so I mucked around some more... and I got it...

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");

spend about 8 hours on this, but i got it!!

I went and basically got a couple of complex xml samples to mess around with. I guess that's how you learn. :)

thanks guys :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号