使用 linq 选择子元素

发布于 2024-10-02 16:00:30 字数 2446 浏览 1 评论 0原文

您好,我几天前刚刚开始研究 LINQ-XML,想知道我是否做错了什么,或者根本不可能做到这一点。我已经搜索过了,没有任何与我相关的问题,我现在已经闲逛了一下。

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。

提前致谢。

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 Men's Large Only.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-10-09 16:00:30

只需将您的查询更改为

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

“这”即可工作,因为您首先获得适用于男士的所有“尺码”商品,然后从该列表中筛选出仅抓取“大号”商品的商品。

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.

尘世孤行 2024-10-09 16:00:30

好吧,我又胡思乱想了一些……我明白了……

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 示例来进行处理。我想这就是你学习的方式。 :)

谢谢大家:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文