如何查看节点有多少个子节点?

Pat*_*ryk 2 c# xml xpath

我试图获得一个节点拥有的子节点数,但我唯一能得到的是是否有任何子节点数不是多少.例如:我在C#中使用Xpath(XPathNodeIterator,XPathDocumentXPathNavigator)

编辑:

iterator.Count
Run Code Online (Sandbox Code Playgroud)

不是我想要实现的,因为它返回表达式返回的所有节点的数量.我想知道有多少个子节点在'下面'iterator.Current

这是我使用的Xml文件(例如)

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<breakfast_menu>
    <food>
        <name>Belgian Waffles</name>
        <price>$5.95</price>
        <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
        <calories>650</calories>
    </food>
    <food>
        <name>Strawberry Belgian Waffles</name>
        <price>$7.95</price>
        <description>light Belgian waffles covered with strawberries and whipped cream</description>
        <calories>900</calories>
    </food>
</breakfast_menu>
Run Code Online (Sandbox Code Playgroud)

`

我的代码:

XPathDocument document = new XPathDocument(@"C:\\xmls\\chair1.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator iterator = navigator.Select("//*");

    while (iterator.MoveNext())
    {
        stringList.Add(iterator.Current.Name);
        if(iterator.Current.HasChildren)   stringList.Add(iterator.Current.Value);
        stringList.Add(" ------- ");
    }
Run Code Online (Sandbox Code Playgroud)

它产生了什么

在此输入图像描述

Rez*_*eni 7

iterator.Current.SelectChildren(XPathNodeType.All).Count
Run Code Online (Sandbox Code Playgroud)