XElement: a collection of all the leaves?
Question
how to get a collection of all the leaves of a XElement tree regardless the hierarchy? Thanks
Solution
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.)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow