Count Total Number of XmlNodes in C#

前端 未结 6 945
走了就别回头了
走了就别回头了 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:26

    If you're doing an unfiltered count, which your question implies, you could just traverse the them using the ChildNodes property:

    private int CountChildren(XmlNode node)
    {
       int total = 0;
    
       foreach (XmlNode child in node.ChildNodes)
       {
          total++;
          total += CountChildren(child);
       }
       return total;
    }
    

提交回复
热议问题