我尝试使用XPathDocument对xml文档中的所有节点进行计数。
im使用的代码是
var xmlPathDoc = new XPathDocument(new StringReader(xml));
XPathNavigator documentNav = xmlPathDoc.CreateNavigator();当我不知道节点的名称时,有没有办法计算这些节点的数量
我想用像这样的东西
int nodeCount = documentNav.Select("/").Count;但不确定要在选择部分中放入什么
谢谢
西蒙
发布于 2012-10-11 23:07:57
XmlNode node = myDoc.SelectSingleNode("/");
int i = node.SelectNodes("descendant::*").Count;发布于 2012-10-11 23:00:02
您可以使用XDocument.Descendents对它们进行计数:
var doc = XDocument.Parse(xml);
var count = doc.Descendants().Count();发布于 2012-10-11 23:01:47
您不需要XPath。首先获取DOM对象,获取所有元素的节点列表并获取其计数。
public virtual XmlNodeList GetElementsByTagName(String tagname )
Returns a NodeList of all the Elements with a given tag name in the order
in which they are encountered in a preorder traversal of the Document tree.
Parameters:
tagname - The name of the tag to match on. The special value "*"
matches all tags.
Returns:
A new NodeList object containing all the matched Elements.https://stackoverflow.com/questions/12842518
复制相似问题