Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XElement: a collection of all the leaves?

Tags:

c#

linq

xelement

how to get a collection of all the leaves of a XElement tree regardless the hierarchy? Thanks

like image 454
pistacchio Avatar asked Jun 24 '09 15:06

pistacchio


1 Answers

Is the Descendants() method what you're after?

That will get all descendants - to get only the leaves, you could use LINQ to Objects with a Where clause:

element.Descendants()
       .Where(desc => !desc.Elements().Any());

(Note this is still only elements, not other nodes like text nodes. Hope that's okay.)

like image 154
Jon Skeet Avatar answered Sep 27 '22 19:09

Jon Skeet