如何获取 XPathNavigator 所在的 XmlNode 的 XPath?
dom4j(一个非常好的Java XML库)有一个非常有用的调用。在其 XPathNavigator 对象的等效项上,您可以请求一个唯一的 Xpath 语句,该语句将为您提供该特定节点。如何使用 .NET XML 库获取此内容?
dom4j (a really good Java XML library) has a very useful call. On the equivalent of their XPathNavigator object you can request a unique Xpath statement that will give you that specific node. How can I get this using the .NET XML library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚解决了这个问题。
注意:这要求 XPathNavigator 对象下面有一个 XmlDocument。您可以有一个不包装 XmlDocument 的 XPathNavigator 对象,对于这些情况,这将不起作用。
要调用此函数,请传入 XPathNavigator 对象当前所在的 XmlNode。它包含在 XPathNavigator.UnderlyingObject 中。
另外,如果您显式设置任何命名空间,则需要传入一个字典,其中键是命名空间 uri,值是前缀。底层 XmlDocument 将使用原始 XML 中定义的命名空间,而不是加载后分配给它的命名空间(不知道为什么)。如果您没有显式设置任何命名空间,则可以传递 null。
完整代码位于 XML Get Unique XPath
I just worked this through.
Note: This requires that there is an XmlDocument underlying the XPathNavigator object. You can have an XPathNavigator object that is not wrapping an XmlDocument and for those cases, this will not work.
To call this, you pass in the XmlNode that the XPathNavigator object is presently on. That is contained in XPathNavigator.UnderlyingObject.
Also, if you explicitly set any namespaces you need to pass in a Dictionary where the key is the namespace uri and the value is the prefix. The underlying XmlDocument will use the namespaces as defined in the original XML, not the namespaces assigned to it after loading it (no idea why). You can pass null if you did not explicitly set any namespaces.
Full code is in a zip at XML Get Unique XPath