You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SelectSingleNode获取SOAP信封节点返回null的原因

问题原因及解决办法

核心问题有两个:

  1. XPath节点名称大小写不匹配
    XML里的SOAP Body节点是s:Body(首字母大写),但你的XPath里写的是s:body(小写),XML节点名称严格区分大小写,这直接导致路径匹配失败。

  2. 未处理Response和result节点的命名空间
    XML中<Response>节点带有xmlns="http://tempuri.org"属性,这意味着Response及其子节点result都属于该命名空间。你的XmlNamespaceManager未为这个命名空间添加前缀,XPath中直接使用Response、result会被解析为无命名空间的节点,自然无法匹配到对应内容。

修正后的代码:

给http://tempuri.org命名空间添加前缀(示例用t),同时修正XPath的大小写和命名空间前缀:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("a", "http://schemas.datacontract.org/2004/07/Dynamics.AX.Application");
nsmgr.AddNamespace("t", "http://tempuri.org"); // 添加tempuri命名空间前缀
                
var value = doc.SelectSingleNode("//s:Envelope/s:Body/t:Response/t:result/a:ExtXLSContract/a:Environement", nsmgr);
// 如需获取节点文本值,可使用 value?.InnerText

注:部分在线XPath工具存在非标准行为(如自动忽略命名空间或大小写),这也是在线测试能成功但代码中失败的原因。

内容的提问来源于stack exchange,提问作者paralleles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 02:35:19