Is there a way to count child nodes on an XDocument?
I looked for a count method or property and could not find one.
Thanks Leo
var doc = XDocument.Load(fileName);
int descendantsCount = doc.Descendants().Count(); // counts ALL descendants elements
int childrenCount = doc.Root.Elements().Count(); // counts direct children of the root element
Alternatively ... if you know that the name of the elements are never going to change and they always exist,
XDocument xD = XDocument.Load(XmlFullFileName);
XElement xE_ParameterSets = xD.Root.Element("Report").Element("ParameterSets");
int index = ((IEnumerable<XElement>)xE_ParameterSets.Elements()).Count();
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