以下两个循环给出不同的输出,尽管它们本质上做的是相同的事情:
XPathNavigator nav = xmlDoc.CreateNavigator();
foreach (object v in (XPathNodeIterator)nav.Evaluate(@"//*[heads!=1]/name"))
{
Console.Out.WriteLine(v);
}
for (int i = 1; i <= 3; i++)
{
Console.Out.Write(i);
Console.Out.WriteLine(nav.Evaluate(string.Format("string(//*[heads!=1][{0}]/name)", i)));
}
输出:
Zaphod
Frankie and Benji
Eddie
1Zaphod
2Frankie and Benji
3
为什么“Eddie”在 for 循环的末尾丢失了?