C# 使用linq选择子元素
您好,几天前我刚刚深入研究了LINQ-XML,我想知道我是做错了什么,还是不可能做到这一点。我到处找了找,没有发现任何与我的有关的问题,我现在也有点胡闹了 XML: 这基本上让我得到了所有的男式颜色样本,但我一直在尝试,看看我是否能得到所有的男式大码颜色样本C# 使用linq选择子元素,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,您好,几天前我刚刚深入研究了LINQ-XML,我想知道我是做错了什么,还是不可能做到这一点。我到处找了找,没有发现任何与我的有关的问题,我现在也有点胡闹了 XML: 这基本上让我得到了所有的男式颜色样本,但我一直在尝试,看看我是否能得到所有的男式大码颜色样本 提前谢谢 只需将查询更改为 var query = from size in ( from catalogItem in cardigan.Descendants("catalog_i
提前谢谢 只需将查询更改为
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");
这将起作用,因为你首先得到所有适用于男性的“尺寸”项目,然后从该列表中筛选出那些只抓取“大”项目的项目。好吧,所以我又找了一些。。。我明白了
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样本来处理。我想这就是你学习的方式
谢谢大家:)你们的意思是
size.Attribute
,而不是size.Attributes
。问题是size是一个IEnumerable而不是XElement,所以它没有属性的定义。我已经试过了:(
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");
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");