<categories>
<category text="Arts">
<category>
<category text="Design"/>
<category text="Visual Arts"/>
<category>
</category>
<category text="Business">
<category>
<category text="Business News"/>
<category text="Careers"/>
<category text="Investing"/>
</category>
</category>
<category text="Comedy"/>
</categories>
目前我正在使用
xDoc.Descendants("category").Where(a => a.Attribute("text").Value == "Arts").Descendants("category")
上面的代码从属性为"艺术"的类别中返回我的所有类别元素我想要的只是属性为"艺术"的类别下方的类别节点不是具有文本属性设计和视觉艺术的类别。我想要整个类别节点,就像下面一样
<category>
<category text="Design"/>
<category text="Visual Arts"/>
<category>
来了,希望你仍然需要这个:
string category = "Business";
var children = xDoc.Root.Elements("category").Where(a => a.Attribute("text").Value == category).Elements();