how to get a collection of all the leaves of a XElement tree regardless the hierarchy? Thanks
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With