tags:

views:

89

answers:

1

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

+4  A: 

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.)

Jon Skeet