Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count Total Number of XmlNodes in C#

Tags:

c#

xml

xpath

I'm trying to find a way to get the total number of child nodes from an XmlNode recursively.

That it is to say I want to count all the children, grand children etc.

I think its something like

node.SelectNodes(<fill in here>).Count

but I don't know what the XPath is.

like image 540
mat-mcloughlin Avatar asked Apr 15 '10 12:04

mat-mcloughlin


2 Answers

XPath supports something called Axis specifier, so the code you're looking for is

node.SelectNodes("descendant::*").Count
like image 91
svick Avatar answered Oct 08 '22 11:10

svick


The XPath you are after is:

descendant::node() (1)

or

descendant::* (2)

The first XPath expresion (1) above selects any node (text-node, processing-instruction, comment, element) in the subtree rooted by the current node.

(2) selects any element node in the subtree rooted by the current node.

like image 43
Dimitre Novatchev Avatar answered Oct 08 '22 11:10

Dimitre Novatchev