Count Total Number of XmlNodes in C#

前端 未结 6 986
走了就别回头了
走了就别回头了 2021-01-13 04:59

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.

6条回答
  •  青春惊慌失措
    2021-01-13 05:18

    I think this will do it for you though not through xPath:

    void CountNode(XmlNode node, ref int count)
    {
        count += node.ChildNodes.Count;
    
        foreach (XmlNode child in node.ChildNodes)
        {
            CountNode(child, ref count);
        }
    }
    

    For reference here is a link to the count function in xpath.

    http://msdn.microsoft.com/en-us/library/ms256103.aspx

    so if you were looking for all the same type of nodes you could do

    //Your_node
    

    to select all the nodes

    //*
    

提交回复
热议问题