0

XPathDocument を使用して xml ドキュメント内のすべてのノードを数えようとしています

私が使用しているコードは

var xmlPathDoc = new XPathDocument(new StringReader(xml));
XPathNavigator documentNav = xmlPathDoc.CreateNavigator();

ノードの名前がわからない場合にこれらをカウントする方法はありますか

私は次のようなものを使いたかった

int nodeCount = documentNav.Select("/").Count;

しかし、選択部分に何を入れるかわかりませんでした

ありがとう

サイモン

4

3 に答える 3

3
XmlNode node = myDoc.SelectSingleNode("/");

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

C# の XmlNodes の総数も参照してください。

于 2012-10-11T15:07:56.800 に答える
2

あなたはXDocument.Descendentsそれらを使用して数えることができます:

var doc = XDocument.Parse(xml);
var count = doc.Descendants().Count();
于 2012-10-11T15:00:01.600 に答える
0

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.
于 2012-10-11T15:01:47.270 に答える