Is there a simple way to get the number of all leaves of an XML string (XML document is provided as a string) with C#?
XDocument xDoc = XDocument.Parse(xml);
var count = xDoc.Descendants().Where(n => !n.Elements().Any()).Count();
or as @sixlettervariables suggested
var count = xDoc.Descendants().Count(e => !e.HasElements);
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