XPathNavigator 是否需要 MoveToParent()/MoveToFirstChild() 对?

发布于 2024-12-16 19:49:12 字数 2186 浏览 0 评论 0原文

这是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-12-23 19:49:12

在这段代码中,当我们遍历完根节点的所有子节点后,就没有更多的工作要做了,根节点不能超过一个。所以不需要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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文