简体   繁体   English

对于XDocument后代操作,如何才能返回直接子节点?

[英]For an XDocument descendants operation how can only return immediate child nodes?

For an XDocument descendants operation how can only return immediate child nodes? 对于XDocument后代操作,如何才能返回直接子节点?

I have an operation along the lines of: 我有一个操作:

XDocument xmlDc = XDocument.Load(dependencyFilePath);    
IEnumerable<IGrouping<string, XElement>> scriptNodes = 
from s in xmlDc.Descendants("script") select s;

The problem is my XML doc is structured as follows 问题是我的XML文档结构如下

<topNode>
 <script>
    <someNode>...</someNode>
    <script>....</script>
 </script>
</topNode>

Essentially, script tags can have child scrip tags, but when I do Descendents, I only want to retrieve immediate child node, not grandchild script nodes. 本质上,脚本标签可以有子脚本标签,但是当我做后代时,我只想检索直接子节点,而不是孙子脚本节点。

Is this possible with XDocument? 这可能与XDocument一起使用吗? Can I use a predicate to somehow check if a potential selectee has for parent the node I am checking from to accomplish this? 我可以使用谓词以某种方式检查潜在的被选择者是否为我正在检查的节点的父节点执行此操作?

我认为Elements("script")可以做到这一点,而不是Descendants("script")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM