How to get specific element Count in XML or XElement variable

不羁的心 提交于 2019-11-28 00:43:24

You can filter the descendant elements using the Descendants method with the name "ID", then count the results:

int count = xml.Descendants("ID").Count();

Be aware that Descendants looks through all levels. If you had an element other than Person that also had an ID child element, you would want to be more specific. In that case, to count ID child elements that belong to Person elements, you would use:

int count = xml.Elements("Person")
               .Elements("ID")
               .Count();
XmlDocument xmldoc = new XmlDocument();
 xmldoc.Load(XmlPath);
var totalItems = xmldoc.SelectNodes(
         "/root/node/LastName/").Count;
var cnt = element.Descendants("ID").Count();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!