XPathNavigator 是否需要 MoveToParent()/MoveToFirstChild() 对?
这是使用 Microsoft 的 XPathNavigator 的示例。
using System;
using System.Xml;
using System.Xml.XPath;
// http://support.microsoft.com/kb/308343
namespace q308343 {
class Class1 {
static void Main(string[] args) {
XPathNavigator nav;
XPathDocument docNav;
docNav = new XPathDocument(@"Books.Xml");
nav = docNav.CreateNavigator();
nav.MoveToRoot();
//Move to the first child node (comment field).
nav.MoveToFirstChild();
do {
//Find the first element.
if (nav.NodeType == XPathNodeType.Element) {
//Determine whether children exist.
if (nav.HasChildren == true) {
//Move to the first child.
nav.MoveToFirstChild();
//Loop through all of the children.
do {
//Display the data.
Console.Write("The XML string for this child ");
Console.WriteLine("is '{0}'", nav.Value);
//Check for attributes.
if (nav.HasAttributes == true) {
Console.WriteLine("This node has attributes");
}
} while (nav.MoveToNext());
}
}
} while (nav.MoveToNext());
//Pause.
Console.ReadLine();
}
}
}
我认为这段代码有一个错误,当没有元素可显示时,它不会执行 MoveToParent()
上升到一级。
nav.MoveToFirstChild();
//Loop through all of the children.
do {
....
} while (nav.MoveToNext());
nav.MoveToParent(); <-- This seems to be missing.
但是,当我编译/执行此示例时,无论有没有 nav.MoveToParent()
,它都可以正常工作。
XPathNavigator 是否需要 MoveToParent()/MoveToFirstChild() 对?是否可以不使用 MoveToParent()
因为当第一次执行 MoveToNext()
时,第二次执行的效果与 MoveToParent()
一样>MoveToNext() 返回 false?
This is an example of using XPathNavigator from Microsoft.
using System;
using System.Xml;
using System.Xml.XPath;
// http://support.microsoft.com/kb/308343
namespace q308343 {
class Class1 {
static void Main(string[] args) {
XPathNavigator nav;
XPathDocument docNav;
docNav = new XPathDocument(@"Books.Xml");
nav = docNav.CreateNavigator();
nav.MoveToRoot();
//Move to the first child node (comment field).
nav.MoveToFirstChild();
do {
//Find the first element.
if (nav.NodeType == XPathNodeType.Element) {
//Determine whether children exist.
if (nav.HasChildren == true) {
//Move to the first child.
nav.MoveToFirstChild();
//Loop through all of the children.
do {
//Display the data.
Console.Write("The XML string for this child ");
Console.WriteLine("is '{0}'", nav.Value);
//Check for attributes.
if (nav.HasAttributes == true) {
Console.WriteLine("This node has attributes");
}
} while (nav.MoveToNext());
}
}
} while (nav.MoveToNext());
//Pause.
Console.ReadLine();
}
}
}
I think this code has a bug that it doesn't execute MoveToParent()
to go up to one level when there is no elements to show.
nav.MoveToFirstChild();
//Loop through all of the children.
do {
....
} while (nav.MoveToNext());
nav.MoveToParent(); <-- This seems to be missing.
However, when I compile/execute this example, it works fine with and without nav.MoveToParent()
.
Is MoveToParent()/MoveToFirstChild() pair necessary with XPathNavigator? Is it OK not to use MoveToParent()
because the second execution of MoveToNext()
works as MoveToParent()
when the first execution of MoveToNext()
returns false?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这段代码中,当我们遍历完根节点的所有子节点后,就没有更多的工作要做了,根节点不能超过一个。所以不需要
MoveToParent()
,我们可以直接退出。这正是代码的作用。In this code, after we go through all of the children of the root node, there is no more work to be done, there can't be more than one root node. So there is no need to
MoveToParent()
, we can just exit. Which is exactly what the code does.