使用 linq 选择子元素
您好,我几天前刚刚开始研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将您的查询更改为
“这”即可工作,因为您首先获得适用于男士的所有“尺码”商品,然后从该列表中筛选出仅抓取“大号”商品的商品。
Simply change your query to be
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.
好吧,我又胡思乱想了一些……我明白了……
花了大约 8 个小时,但我明白了!
我基本上得到了一些复杂的 xml 示例来进行处理。我想这就是你学习的方式。 :)
谢谢大家:)
ok so I mucked around some more... and I got it...
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 :)