c# - 如何检查一个节点有多少个 child ?

标签 c# xml xpath

我正在尝试获取一个节点拥有的子节点的数量,但我唯一能得到的是它是否有任何子节点,而不是有多少。例如 : 我在 C# 中使用 Xpath(XPathNodeIteratorXPathDocumentXPathNavigator)

编辑:

iterator.Count

不是我想要实现的,因为它返回表达式返回的所有节点的数量。我想知道 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>

`

我的代码:

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(" ------- ");
    }

它产生了什么

enter image description here

最佳答案

iterator.Current.SelectChildren(XPathNodeType.All).Count

关于c# - 如何检查一个节点有多少个 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8281696/

相关文章:

python - Xpath 不匹配

java - 选择给定节点之后的节点

c# - C#中 `event = method`是什么意思

c# - OracleParameter.Size 有什么影响?

c# - 缩小 Asp.Net 应用程序的 Html

c# - 无法获取元素

python - 查找并保存在列表中具有相同名称的 XML 标签,Python

python - Scrapy - 发送新请求/使用回调

c# - 如何从asp.net中动态生成的复选框列表中获取复选框值

xml - 无法在新的 ApachePOI XSSFWorkbook 中简单地创建主题