SelectSingleNode 返回 null - 即使有命名空间
我知道以前曾以类似的方式问过这个问题,但我似乎无法解决这个问题。
我有一些 xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<Research xmlns="http://www.rixml.org/2005/3/RIXML" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" createDateTime="2011-03-29T15:41:48Z" language="eng" researchID="MusiJvs3008">
<Product productID="MusiJvs3008">
<StatusInfo currentStatusIndicator="Yes" statusDateTime="2011-03-29T15:41:48Z" statusType="Published" />
<Source>
<Organization type="SellSideFirm" primaryIndicator="Yes">
<OrganizationID idType="Reuters">9999</OrganizationID>
并且我正在尝试使用 xpath: 读取值:
XPathDocument xmldoc = new XPathDocument(xmlFile);
XPathNavigator nav = xmldoc.CreateNavigator();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace(string.Empty, "http://www.rixml.org/2005/3/RIXML");
XPathNavigator result = nav.SelectSingleNode("/Research", nsMgr); // <-- Returns null!
但即使是简单的根节点选择也会返回 null!我确信我的命名空间有问题。有人可以帮忙吗?
理想情况下,我想要简单的行,让我从 xml 文件中选择值,即
String a = xmlDoc.SelectSingleNode(@"/Research/Product/Content/Title").Value;
顺便说一句,我无法(直接)控制 XML 文件内容。
I know this question has been asked in a similar fashion before, but I can't seem to get this working.
I have some xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<Research xmlns="http://www.rixml.org/2005/3/RIXML" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" createDateTime="2011-03-29T15:41:48Z" language="eng" researchID="MusiJvs3008">
<Product productID="MusiJvs3008">
<StatusInfo currentStatusIndicator="Yes" statusDateTime="2011-03-29T15:41:48Z" statusType="Published" />
<Source>
<Organization type="SellSideFirm" primaryIndicator="Yes">
<OrganizationID idType="Reuters">9999</OrganizationID>
And I'm trying to read values using xpath:
XPathDocument xmldoc = new XPathDocument(xmlFile);
XPathNavigator nav = xmldoc.CreateNavigator();
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace(string.Empty, "http://www.rixml.org/2005/3/RIXML");
XPathNavigator result = nav.SelectSingleNode("/Research", nsMgr); // <-- Returns null!
But even a simple select of the root node returns null! I am sure I have something wrong with my namespace. Can someone please help?
Ideally I want simple lines that will let me select values from the xml file, i.e.
String a = xmlDoc.SelectSingleNode(@"/Research/Product/Content/Title").Value;
BTW, I have no (direct) control over the XML file content.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不相信您可以使用空的命名空间别名并让 XPath 表达式自动使用它。一旦您使用实际的别名,它就应该可以工作。这个测试很好,例如:
顺便问一下,您有使用 XPath 和
XPathDocument
吗?我倾向于发现 LINQ to XML 是一个令人愉快得多的 API,特别是在涉及命名空间时。如果您使用 .NET 3.5 并且没有使用 XPath 的特殊要求,我建议您检查一下。I don't believe you can use an empty namespace alias and have it used automatically by the XPath expression. As soon as you use an actual alias, it should work though. This test is fine, for example:
Do you have to use XPath and
XPathDocument
, by the way? I tend to find that LINQ to XML is a much more pleasant API, particularly when it comes to namespaces. If you're using .NET 3.5 and you have no particular requirement to use XPath, I'd suggest you check it out.进行以下更改
Make the following changes
对于此代码的本机等效项,我可以发布并回答我自己的问题。相反,我会将其添加为最后的答案。
使用本机
IXMLDOMDocument
(版本 6)对象时:额外问题:为什么没有 Xml 文档对象模型查询默认命名空间当没有指定命名空间时...叹息
i could post, and answer my own question, for the native equivalent of this code. Instead i'll just add it as an answer to the end.
When using the native
IXMLDOMDocument
(version 6) object:Bonus Question: Why, oh why, does no Xml Document object model query the default namespace when no namespace is specified... sigh