我正在尝试获取一个节点拥有的子节点的数量,但我唯一能得到的是它是否有任何子节点,而不是有多少。例如 :
我在 C# 中使用 Xpath(XPathNodeIterator
、XPathDocument
和 XPathNavigator
)
编辑:
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(" ------- ");
}
它产生了什么
最佳答案
iterator.Current.SelectChildren(XPathNodeType.All).Count
关于c# - 如何检查一个节点有多少个 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8281696/