Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 0.32 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Count the leaves of an XML string with C#
  2. XDocument xDoc = XDocument.Parse(xml);
  3. var count = xDoc.Descendants().Where(n => !n.Elements().Any()).Count();
  4.        
  5. var count = xDoc.Descendants().Count(e => !e.HasElements);
  6.        
  7. XmlDocument doc = new XmlDocument();
  8. doc.LoadXml("...");
  9. int count = doc.SelectNodes("//*[not(*)]").Count;