Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq-to-xml

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

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

<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? Can I use a predicate to somehow check if a potential selectee has for parent the node I am checking from to accomplish this?

like image 496
Matt Avatar asked Jul 23 '10 00:07

Matt


1 Answers

I think Elements("script") would do the trick, instead of Descendants("script")

like image 87
kiwipom Avatar answered Sep 29 '22 19:09

kiwipom