使用 XPathNodeIterator 仅选择子节点后,我的 InnerXML 等于我的 OuterXML

发布于 2024-10-06 21:15:15 字数 1053 浏览 1 评论 0原文

我使用的代码是:

string m_myXML = "<parent>\n" +
                 "  <child1>\n"+
                 "    <child2a>\n"+
                 "      <list1 attrib=\"one\" />\n"+
                 "      <list2 attrib=\"two\" />\n"+
                 "    </child2a>\n"+
                 "    <child2b>\n"+
                 "      <list1 attrib=\"one\" />\n"+
                 "      <list2 attrib=\"two\" />\n"+
                 "    </child2b>\n"+
                 "  </child1>\n"+
                 "</parent>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(m_myXML);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr;
expr = nav.Compile("/*/*"); //Select all children of top level parents
XPathNodeIterator iterator = nav.Select(expr);

结果是 iterator.Current.InnerXml 与 iterator.Current.OuterXml 相同,这与原始 m_myXML 相同。当我通过 iterator.MoveNext() 将迭代器移动到下一个迭代器时,它指向第一个 child1 - 这就是我一开始所期望的。

我做错了什么吗?对于 System.Xml 等应该如何运作的假人,是否有一个很好且详细的解释?

The Code i am using is:

string m_myXML = "<parent>\n" +
                 "  <child1>\n"+
                 "    <child2a>\n"+
                 "      <list1 attrib=\"one\" />\n"+
                 "      <list2 attrib=\"two\" />\n"+
                 "    </child2a>\n"+
                 "    <child2b>\n"+
                 "      <list1 attrib=\"one\" />\n"+
                 "      <list2 attrib=\"two\" />\n"+
                 "    </child2b>\n"+
                 "  </child1>\n"+
                 "</parent>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(m_myXML);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr;
expr = nav.Compile("/*/*"); //Select all children of top level parents
XPathNodeIterator iterator = nav.Select(expr);

The result is that iterator.Current.InnerXml is the same as iterator.Current.OuterXml and this is the same as the original m_myXML. When i Move the iterator to the next via iterator.MoveNext() it points to the first child1 - wich is what i would expect from it right at the beginning.

Am i doing something wrong? Is there a good and detailed explanation for dummys out there how System.Xml etc is supposed to function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

錯遇了你 2024-10-13 21:15:15

正如 Martin Honnen 指出的:
一个很好的来源是 msdn/system.xml... “XPathNavigator 类返回的 XPathNodeIterator 对象未定位在选定节点集中的第一个节点上。必须调用 XPathNodeIterator 类的 MoveNext 方法才能将 XPathNodeIterator 对象定位在选定节点集中的第一个节点上。选定的节点集。”

这使得 perfekt 现在变得有意义 - 因为否则你将很难使用这样的东西迭代列表:

while (iterator.MoveNext())
{
    //Do Stuff
}

谢谢 Martin Honnen 评论部分的答案 - 我完全错过了那堂课的要点。

As Martin Honnen pointed out:
A good source is msdn/system.xml... "An XPathNodeIterator object returned by the XPathNavigator class is not positioned on the first node in a selected set of nodes. A call to the MoveNext method of the XPathNodeIterator class must be made to position the XPathNodeIterator object on the first node in the selected set of nodes."

This makes perfekt Sense now - because otherwise you would have trouble to iterate through the List using something like this:

while (iterator.MoveNext())
{
    //Do Stuff
}

Thank you Martin Honnen for your Answer in the comment section - i totally missed the point on that class.

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