【问题标题】:SelectSingleNode return null - even with namespaceSelectSingleNode 返回 null - 即使使用命名空间
【发布时间】:2023-03-07 11:12:01
【问题描述】:

我知道以前有人以类似的方式提出过这个问题,但我似乎无法解决这个问题。

我有一些 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 文件的内容。

【问题讨论】:

    标签: c# .net xml namespaces searching-xml


    【解决方案1】:

    我不相信您可以使用空的命名空间别名并让 XPath 表达式自动使用它。只要您使用实际别名,它就应该可以工作。这个测试很好,例如:

    using System;
    using System.Xml;
    using System.Xml.XPath;
    
    class Test
    {
        static void Main() 
        {
            string xmlFile = "test.xml";
            XPathDocument xmldoc = new XPathDocument(xmlFile); 
            XPathNavigator nav = xmldoc.CreateNavigator(); 
            XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
            nsMgr.AddNamespace("x", "http://www.rixml.org/2005/3/RIXML"); 
            XPathNavigator result = nav.SelectSingleNode("/x:Research", nsMgr);
            Console.WriteLine(result);
        }
    }
    

    顺便说一句,您使用 XPath 和 XPathDocument 吗?我倾向于发现 LINQ to XML 是一个非常更令人愉快的 API,尤其是在涉及命名空间时。如果您使用的是 .NET 3.5,并且对使用 XPath 没有特别要求,我建议您检查一下。

    【讨论】:

    • 很好的答案,非常感谢您的帮助。我想使用 LINQ,但我对调试的影响感到恼火,即对 lamda 表达式的任何更改都意味着您必须重新启动整个过程。再次感谢!瑞安
    • @Ryan:你花很多时间在调试器上吗?我很少使用编辑并继续 - 我只是重新编译并再次运行单元测试。
    • 永远!我发现开发的停止/启动非常缓慢,特别是如果它是一个 ASP 应用程序。
    • @Ryan:那么你的单元测试覆盖率是什么样的?即使在 Web 应用程序中,大部分您的重要代码都应该是可测试的......并且运行单元测试应该很快。
    • @JonSkeet 我尝试清除该字段然后重新填充,但它使整个节点消失:stackoverflow.com/questions/31081361/…。请帮忙。
    【解决方案2】:

    进行以下更改

    nsMgr.AddNamespace("x", "http://www.rixml.org/2005/3/RIXML"); 
    XPathNavigator result = nav.SelectSingleNode("/x:Research", nsMgr);
    

    【讨论】:

      【解决方案3】:

      我可以发布并回答我自己的问题,以获得此代码的 native 等效项。相反,我将其添加为最后的答案。

      使用原生IXMLDOMDocument(版本6)对象时:

      //Give the default namespace as alias of "x"
      document.setProperty("SelectionNamespaces","xmlns:x='http://www.rixml.org/2005/3/RIXML'");
      
      //Query for the nodes we want
      document.selectSingleNode("/x:Research");
      

      额外问题:为什么,哦,为什么,当没有指定命名空间时,没有 Xml 文档对象模型查询 default 命名空间...... 叹息

      【讨论】:

        猜你喜欢
        • 2016-06-11
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 2020-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多