Default namespace not working in Dotnet API.

本文探讨了使用XPath查询带有默认命名空间的XML文件时遇到的问题,并提供了解决方案。通过一个具体的例子说明了如何正确地指定命名空间前缀。
As in one of my post in some other site, I discuss the possibility of working with Xml Linq with XPath. However, let see if we have a XML file that has thefollowing contents.

<?xml version="1.0" encoding="utf-8"?>
<Report Id="ID1" Type="Demo Report" Created="2011-01-01T01:01:01+11:00" Culture="en" xmlns="http://demo.com/2011/demo-schema">
    <ReportInfo>
        <Name>Demo Report</Name>
        <CreatedBy>Unit Test</CreatedBy>
    </ReportInfo>
</Report>


as you can see the it has default xmlns set to something other than empty.

<Report ... xmlns="http://demo.com/2011/demo-schema" > 

So, if you think that you can create a XmlNamespaceManager, and add the string.Empty as the key and the URI in the default namespace as the above xml. And then you construct the Xpath and query the Xpath result. 


var xmlns = new XmlNamespaceManager(new NameTable());
      xmlns.AddNamespace(string.Empty, "http://demo.com/2011/demo-schema");
      var xelements = xdoc.XPathSelectElements("/Report/ReportInfo/Name", xmlns);

You may get a empty result. (it is not null as you might expect the result to be when no element that match that criteria).

 Assert.AreEqual(0, xelements.Count());

Here is the source that verify that. 


[Test]
    public void TestStackOverflow_Example()
    {
      var xmlstring = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Report Id=""ID1"" Type=""Demo Report"" Created=""2011-01-01T01:01:01+11:00"" Culture=""en"" xmlns=""http://demo.com/2011/demo-schema"">
    <ReportInfo>
        <Name>Demo Report</Name>
        <CreatedBy>Unit Test</CreatedBy>
    </ReportInfo>
</Report>";

      // the XML without namespace is working fine with the Xpath string. however, what is missing in the Xpath one?
//      var xmlstring = @"<?xml version=""1.0"" encoding=""utf-8""?>
//<Report Id=""ID1"" Type=""Demo Report"" Created=""2011-01-01T01:01:01+11:00"" Culture=""en"" >
//    <ReportInfo>
//        <Name>Demo Report</Name>
//        <CreatedBy>Unit Test</CreatedBy>
//    </ReportInfo>
//</Report>";


      var buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(xmlstring);
      var memory = new MemoryStream(buffer);
      memory.Seek(0, SeekOrigin.Begin);

      var xdoc = XDocument.Load(memory);
      Console.WriteLine("xdoc = \n" + xdoc.ToString());
      var xmlns = new XmlNamespaceManager(new NameTable());
      xmlns.AddNamespace(string.Empty, "http://demo.com/2011/demo-schema");
      Assert.AreEqual("http://demo.com/2011/demo-schema", xmlns.DefaultNamespace);

      var xelements = xdoc.XPathSelectElements("/Report/ReportInfo/Name", xmlns);
      Assert.IsNotNull(xelements);
      Assert.AreEqual(0, xelements.Count());

      // This shows the correct way to do the Xpath query when Default XNamespace is not supported because of 
      // XPath 1.0 does not support default namespace
      xmlns.AddNamespace("empty", "http://demo.com/2011/demo-schema");
      var xelements2 = xdoc.XPathSelectElements("/empty:Report/empty:ReportInfo/empty:Name", xmlns);
      Assert.IsNotNull(xelements2);
      Assert.AreEqual(1, xelements2.Count());

    }



So what is the reason? From the post in stack overflow.  in quoted.

<quoted>

When using XPath in .NET (via a navigator or SelectNodes/SelectSingleNode) on XML with namespaces you need to:

  • provide your own XmlNamespaceManager

  • and explicitly prefix all elements in XPath expression, which are in namespace.

The latter is (paraphrased from MS source linked below): because XPath 1.0 ignores default namespace (xmlns="some_namespace") specifications. So when you use element name without prefix it assumes null namespace.

That's why .NET implementation of XPath ignores namespace with prefix String.Empty in XmlNamespaceManager and allways uses null namespace.

See XmlNamespaceManager and UndefinedXsltContext don't handle default namespace for more information.

I find this "feature" very inconvenient because you cannot make old XPath namespace-aware by simply adding default namespace declaration, but that's how it works.

</quoted>


So , so the remedy for this non-empty default namespace, you have to qualify the names with certain prefix strings. As you might already find out in the Test code above.

PS C:\Users\Administrator> Set-ExecutionPolicy RemoteSigned -Scope LocalMachine -Force >> Set-ExecutionPolicy RemoteSigned -Scope LocalMachine -Force >> PS C:\Users\Administrator> cd E:\ >> .\Setup-Environment.ps1 >> 涓嬭浇 Node.js 瀹夎绋嬪簭... Invoke-WebRequest : 文件“E:\ai_temp\node-installer.msi”正由另一进程使用,因此该进程无法访问此文件。 所在位置 E:\Setup-Environment.ps1:12 字符: 1 + Invoke-WebRequest -Uri $nodeUrl -OutFile $nodeInstaller -UseBasicPars ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand 姝e湪瀹夎 Node.js... 涓嬭浇 .NET SDK 瀹夎绋嬪簭... Invoke-WebRequest : 远程服务器返回错误: (400) 错误的请求。 所在位置 E:\Setup-Environment.ps1:20 字符: 1 + Invoke-WebRequest -Uri $dotnetUrl -OutFile $dotnetInstaller -UseBasic ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest],WebExce ption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand 姝e湪瀹夎 .NET SDK... Start-Process : 由于出现以下错误,无法运行此命令: 系统找不到指定的文件。。 所在位置 E:\Setup-Environment.ps1:22 字符: 1 + Start-Process $dotnetInstaller -Wait -ArgumentList "/install", "/quie ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Start-Process],InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand 璁剧疆绯荤粺鐜鍙橀噺... node : 无法将“node”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确 ,然后再试一次。 所在位置 E:\Setup-Environment.ps1:33 字符: 32 + Write-Host "楠岃瘉 Node.js 瀹夎: $(node --version)" + ~~~~ + CategoryInfo : ObjectNotFound: (node:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 楠岃瘉 Node.js 瀹夎: dotnet : 无法将“dotnet”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径 正确,然后再试一次。 所在位置 E:\Setup-Environment.ps1:34 字符: 33 + Write-Host "楠岃瘉 .NET SDK 瀹夎: $(dotnet --version)" + ~~~~~~ + CategoryInfo : ObjectNotFound: (dotnet:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 楠岃瘉 .NET SDK 瀹夎: 鉁?鐜璁剧疆瀹屾垚 PS E:\> .\Install-EcosystemService.ps1 -Action Uninstall ✅ 服务停止成功 __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 0 PSComputerName : ✅ 服务删除成功 PS E:\> Start-Sleep -Seconds 2 PS E:\> .\Install-EcosystemService.ps1 -Action Install ✅ 服务创建成功 ✅ 服务权限设置成功 ❌ 服务启动失败: 由于以下错误无法启动服务“Project Ecosystem Manager (ProjectEcosystemManager)”: 无法启动计算机“.”上 的服务 ProjectEcosystemManager。 === 诊断信息 === 服务路径: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "E:\ProjectEcosystem\ProjectMonitor\Scripts\ProjectEcosystemManager.ps1" 测试命令: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "E:\ProjectEcosystem\ProjectMonitor\Scripts\ProjectEcosystemManager.ps1" -TestMode PS E:\> .\Install-EcosystemService.ps1 -Action Uninstall >> Start-Sleep -Seconds 2 >> .\Install-EcosystemService.ps1 -Action Install >> ✅ 服务停止成功 __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 0 PSComputerName : ✅ 服务删除成功 ✅ 服务创建成功 ✅ 服务权限设置成功 ❌ 服务启动失败: 由于以下错误无法启动服务“Project Ecosystem Manager (ProjectEcosystemManager)”: 无法启动计算机“.”上 的服务 ProjectEcosystemManager。 === 诊断信息 === 服务路径: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "E:\ProjectEcosystem\ProjectMonitor\Scripts\ProjectEcosystemManager.ps1" 测试命令: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File "E:\ProjectEcosystem\ProjectMonitor\Scripts\ProjectEcosystemManager.ps1" -TestMode PS E:\> Start-Service -Name "ProjectEcosystemManager" >> Get-Service -Name "ProjectEcosystemManager" >> Get-Content "E:\ProjectEcosystem\Logs\Service_*.log" -Tail 50 -Wait >> Start-Service : 由于以下错误无法启动服务“Project Ecosystem Manager (ProjectEcosystemManager)”: 无法启动计算机“.”上 的服务 ProjectEcosystemManager。 所在位置 行:1 字符: 1 + Start-Service -Name "ProjectEcosystemManager" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand Status Name DisplayName ------ ---- ----------- Stopped ProjectEcosyste... Project Ecosystem Manager 工作目录: C:\Users\Administrator PATH环境变量: C:\Windows\System32;E:\curl-8.15.0_4-win64-mingw\bin;E:\Python310;E:\Python310\Scripts;C:\Windows;\Wbem;\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;E:\ai_temp;C:\Program Files\NVIDIA Corporation\NVIDIA App\NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ProgramData\Miniconda3\Scripts;C:\ProgramData\Miniconda3\Library\bin;C:\Program Files\Microsoft VS Code\bin;E:\Python310;E:\Python310\Scripts;E:\ai_temp ⚠️ 使用默认监控间隔: 60秒 ⏱️ 等待 60 秒后再次检查... ⚠️ 使用默认监控间隔: 60秒 ⏱️ 等待 60 秒后再次检查... ⚠️ 使用默认监控间隔: 60秒 ⏱️ 等待 60 秒后再次检查... ⚠️ 使用默认监控间隔: 60秒 ⏱️ 等待 60 秒后再次检查... ⚠️ 使用默认监控间隔: 60秒 ⏱️ 等待 60 秒后再次检查... ⚠️ 使用默认监控间隔: 60秒 ⏱️ 等待 60 秒后再次检查... ⚠️ 使用默认监控间隔: 60秒 ⏱️ 等待 60 秒后再次检查... ⚠️ 使用默认监控间隔: 60秒 ⏱️ 等待 60 秒后再次检查... ⚠️ 使用默认监控间隔: 60秒 ⏱️ 等待 60 秒后再次检查... ********************** Windows PowerShell 脚本开始 开始时间: 20250813215212 用户名: BF-202503252000\Administrator RunAs 用户: BF-202503252000\Administrator 配置名称: 计算机: BF-202503252000 (Microsoft Windows NT 10.0.22631.0) 主机应用程序: C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe 进程 ID: 4620 PSVersion: 5.1.22621.2506 PSEdition: Desktop PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.22621.2506 BuildVersion: 10.0.22621.2506 CLRVersion: 4.0.30319.42000 WSManStackVersion: 3.0 PSRemotingProtocolVersion: 2.3 SerializationVersion: 1.1.0.1 ********************** 已启动脚本,输出文件为 E:\ProjectEcosystem\Logs\Service_20250813.log === 生态系统管理服务启动于 08/13/2025 21:52:12 === 当前用户: BF-202503252000\Administrator 工作目录: C:\Users\Administrator PATH环境变量: C:\Windows\System32;E:\curl-8.15.0_4-win64-mingw\bin;E:\Python310;E:\Python310\Scripts;C:\Windows;\Wbem;\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;E:\ai_temp;C:\Program Files\NVIDIA Corporation\NVIDIA App\NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ProgramData\Miniconda3\Scripts;C:\ProgramData\Miniconda3\Library\bin;C:\Program Files\Microsoft VS Code\bin;E:\Python310;E:\Python310\Scripts;E:\ai_temp ⚠️ 配置文件不存在,使用默认配置 PS>TerminatingError(Out-File):“未能找到路径“E:\ProjectEcosystem\Config\EcosystemConfig.xml”的一部分。” ❌ 配置加载失败: 未能找到路径“E:\ProjectEcosystem\Config\EcosystemConfig.xml”的一部分。 ********************** Windows PowerShell 脚本结束 结束时间: 20250813215212 **********************
最新发布
08-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值