无法让 .NET XPathNavigator 工作
我在使用 XPathNavigator 时遇到问题。我有一个文档,其中有一堆“主题”元素,流中没有命名空间。
我正在使用(表达简化到最低限度,首先我认为我的表达是错误的):
XPathDocument xmlDoc = new XPathDocument( stream );
XPathNavigator xml = xmlDoc.CreateNavigator();
XPathNodeIterator iter = xml.Select( "//topic" );
这不起作用。我可以选择 */*/* 或类似的内容并让我的“主题”元素正常。 我尝试在在线测试器和其他语言中运行我的表达式,它们起作用了。
问题:怎么了?我一直怀疑它与该死的 NamespaceManager 对象有关,每次我用命名空间解析文档时,这都会给我带来难以置信的痛苦,但这次我正在寻找的元素没有显式命名空间! 我添加:
XmlNamespaceManager s = new XmlNamespaceManager( xml.NameTable );
并将其作为第二个参数传递给 Select - 无济于事。我应该如何向这个东西添加“”名称空间/正确使用它?
或者,更好的是,有没有办法在 .NET 中使用 XPath,而不像在其他语言中那样使用这种可怕的令人厌恶的类?如果我想要命名空间,我可以将它们写在表达式中...
更新: 我想出了一个解决方法 - 从根节点复制/粘贴默认 xmlns,然后使用该名称空间:
thisIsRetarded.AddNamespace( "x", "urn:xmind:xmap:xmlns:content:2.0" );
XPathNodeIterator projectIter = projectTree.Select( "//x:topic", thisIsRetarded );
但是,我不应该知道默认 URI,也不喜欢用不必要的 x:-s 污染我的表达式。所以我现在只需要回答问题的第二部分。
I am having problems with XPathNavigator. I have a document with a bunch of "topic" elements w/o namespace in a stream.
I am using (expression dumbed down to bare minimum, first I thought my expressions were wrong):
XPathDocument xmlDoc = new XPathDocument( stream );
XPathNavigator xml = xmlDoc.CreateNavigator();
XPathNodeIterator iter = xml.Select( "//topic" );
This doesn't work. I can select */*/*
or something similar and get my "topic" elements alright.
I tried running my expressions in online tester and other languages and they work.
Question: what's wrong? I have lingering suspicion that it has to do with accursed NamespaceManager object, which causes me incredible pain every time I parse document with namespaces, but this time the elements I am seeking don't have an explicit namespace!
I added:
XmlNamespaceManager s = new XmlNamespaceManager( xml.NameTable );
and pass that as 2nd argument to Select - to no avail. How am I supposed to add "" namespace to this thing/use it correctly?
Or, better yet, is there way to use XPath in .NET without using this horrible abomination of the class, like in other languages? If I want namespaces, I can write them in the expression...
Update:
I figured out a workaround- copy/paste default xmlns from root node, and then use that namespace:
thisIsRetarded.AddNamespace( "x", "urn:xmind:xmap:xmlns:content:2.0" );
XPathNodeIterator projectIter = projectTree.Select( "//x:topic", thisIsRetarded );
however, neither I am supposed to know the default URI, nor do I like to pollute my expressions with unnecessary x:-s. So I only need answer to 2nd part of the question now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我更喜欢使用
XmlDocument
:I prefer to use
XmlDocument
:检查此 StackOverflow 对同一问题的回答以及默认命名空间和无命名空间之间的解释:
具有默认值的 Xml-SelectNodes-通过 XmlNamespaceManager 的命名空间未按预期工作
Microsoft 对如何使用默认命名空间提供了更好的解释:
https://learn.microsoft。 com/en-us/dotnet/standard/data/xml/xpath-queries-and-namespaces
Check this StackOverflow answer to the same problem with the explanation between the default namespace and no namespace:
Xml-SelectNodes with default-namespace via XmlNamespaceManager not working as expected
And an even better explanation by Microsoft on how to use the default namespace:
https://learn.microsoft.com/en-us/dotnet/standard/data/xml/xpath-queries-and-namespaces