- Count the leaves of an XML string with C#
- XDocument xDoc = XDocument.Parse(xml);
- var count = xDoc.Descendants().Where(n => !n.Elements().Any()).Count();
- var count = xDoc.Descendants().Count(e => !e.HasElements);
- XmlDocument doc = new XmlDocument();
- doc.LoadXml("...");
- int count = doc.SelectNodes("//*[not(*)]").Count;