无法让 .NET XPathNavigator 工作

发布于 2024-08-17 13:43:09 字数 1001 浏览 3 评论 0原文

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

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

发布评论

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

评论(2

时光无声 2024-08-24 13:43:09

我更喜欢使用 XmlDocument

XmlDocument doc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("sample", "...");
doc.Load(stream);

XmlNode topic = doc.SelectSingleNode("/sample:topic", nsmgr);

// If you don't have any namespaces....
XmlNode topic2 = doc.SelectSingleNode("/topic"); 

I prefer to use XmlDocument:

XmlDocument doc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("sample", "...");
doc.Load(stream);

XmlNode topic = doc.SelectSingleNode("/sample:topic", nsmgr);

// If you don't have any namespaces....
XmlNode topic2 = doc.SelectSingleNode("/topic"); 
旧城空念 2024-08-24 13:43:09

检查此 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

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