首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有XPathDocument的节点数

具有XPathDocument的节点数
EN

Stack Overflow用户
提问于 2012-10-11 22:55:30
回答 3查看 187关注 0票数 0

我尝试使用XPathDocument对xml文档中的所有节点进行计数。

im使用的代码是

代码语言:javascript
复制
var xmlPathDoc = new XPathDocument(new StringReader(xml));
XPathNavigator documentNav = xmlPathDoc.CreateNavigator();

当我不知道节点的名称时,有没有办法计算这些节点的数量

我想用像这样的东西

代码语言:javascript
复制
int nodeCount = documentNav.Select("/").Count;

但不确定要在选择部分中放入什么

谢谢

西蒙

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-11 23:07:57

代码语言:javascript
复制
XmlNode node = myDoc.SelectSingleNode("/");

int i = node.SelectNodes("descendant::*").Count;

另请参阅Count Total Number of XmlNodes in C#

票数 3
EN

Stack Overflow用户

发布于 2012-10-11 23:00:02

您可以使用XDocument.Descendents对它们进行计数:

代码语言:javascript
复制
var doc = XDocument.Parse(xml);
var count = doc.Descendants().Count();
票数 2
EN

Stack Overflow用户

发布于 2012-10-11 23:01:47

您不需要XPath。首先获取DOM对象,获取所有元素的节点列表并获取其计数。

代码语言:javascript
复制
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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12842518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档