![](/img/trans.png)
[英]Linq to Xml : highlighting respective child nodes upon selecting parent node
[英]LINQ XML obtaining child nodes of parent node
请在下面找到xml数据。 考虑到我知道父节点具有url =“#”,title =“ item 1”和key =“ Item1”,我想使用LINQ查询获取具有键“ Sub1”和“ Sub2”的节点。
<root>
<node title="Partner" url="" description="99" roles="GuidIdHere"></node>
<node title="Test" url="" description="51" roles="8b0c0c4f" key="sample">
<node url="#" title="item 1" description="" key="Item1">
<node url="#" title="Sub 1" description="" key="Sub1" />
<node url="#" title="Sub 2" description="" key="Sub2" />
</node>
<node url="#" title="item 2" description="" key="Item2" />
<node url="#" title="item 3" description="" key="Item3" />
</node>
</root>
谢谢!
var result = from x in doc.Root.Descendents("node")
where (string)x.Attribute("url") == "#"
&& (string)x.Attribute("title") == "item 1"
&& (string)x.Attribute("key") == "Item1"
from y in x.Elements("node")
where (string)x.Attribute("key") == "Sub1"
|| (string)x.Attribute("key") == "Sub2"
select y;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.