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

在C#中使用XmlNamespaceManager访问带命名空间的SOAP响应元素

访问SOAP响应元素的解决方案

针对你提供的SOAP响应,下面给出两种常用的.NET处理方式,帮助你获取响应中的所有元素:

方法一:使用XmlDocument + XmlNamespaceManager

这是传统的XML处理方式,适合熟悉XPath的场景:

// 假设soapResponse是你的SOAP响应字符串
string soapResponse = @"<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
    <soap:Header/>
    <soap:Body>
        <Result>
            <statuscode>200</statuscode>
            <Customer>10001252</Customer>
            <CustomerName>MBOKO FARMS</CustomerName>
            <CustomerFullName>MBOKO FARMS/0 ANAMBRA</CustomerFullName>
            <FiscalAddress/>
            <VATRegistration/>
        </Result>
    </soap:Body>
</soap:Envelope>";

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

// 创建命名空间管理器,注册SOAP命名空间
XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");

// 通过XPath定位到Result节点(必须指定soap命名空间前缀)
XmlNode resultNode = doc.SelectSingleNode("/soap:Envelope/soap:Body/Result", nsManager);

if (resultNode != null)
{
    // 逐个获取子元素值,注意处理可能为空的元素
    string statusCode = resultNode.SelectSingleNode("statuscode")?.InnerText;
    string customerId = resultNode.SelectSingleNode("Customer")?.InnerText;
    string customerName = resultNode.SelectSingleNode("CustomerName")?.InnerText;
    string customerFullName = resultNode.SelectSingleNode("CustomerFullName")?.InnerText;
    string fiscalAddress = resultNode.SelectSingleNode("FiscalAddress")?.InnerText ?? string.Empty;
    string vatRegistration = resultNode.SelectSingleNode("VATRegistration")?.InnerText ?? string.Empty;

    // 示例输出
    Console.WriteLine($"状态码: {statusCode}");
    Console.WriteLine($"客户ID: {customerId}");
    Console.WriteLine($"客户名称: {customerName}");
    Console.WriteLine($"客户全称: {customerFullName}");
    Console.WriteLine($"财务地址: {fiscalAddress}");
    Console.WriteLine($"VAT注册信息: {vatRegistration}");
}

关键说明:

  • SOAP的Envelope和Body元素属于http://schemas.xmlsoap.org/soap/envelope/命名空间,必须通过XmlNamespaceManager注册该命名空间的前缀(这里用soap,和响应中的前缀一致),XPath查询时才能正确识别这些元素。
  • Result及其子元素没有命名空间,查询时直接使用元素名即可,无需添加前缀。
  • 使用SelectSingleNode时,必须传入nsManager作为第二个参数,否则命名空间前缀无法被解析。

方法二:使用LINQ to XML(XDocument)

这是.NET中更简洁的XML处理方式,适合现代开发场景:

string soapResponse = @"<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
    <soap:Header/>
    <soap:Body>
        <Result>
            <statuscode>200</statuscode>
            <Customer>10001252</Customer>
            <CustomerName>MBOKO FARMS</CustomerName>
            <CustomerFullName>MBOKO FARMS/0 ANAMBRA</CustomerFullName>
            <FiscalAddress/>
            <VATRegistration/>
        </Result>
    </soap:Body>
</soap:Envelope>";

XDocument xDoc = XDocument.Parse(soapResponse);
// 定义SOAP命名空间
XNamespace soapNs = "http://schemas.xmlsoap.org/soap/envelope/";

// 定位到Result元素
XElement resultElement = xDoc.Descendants(soapNs + "Body")
                            .Elements("Result")
                            .FirstOrDefault();

if (resultElement != null)
{
    // 获取各子元素值,空元素自动返回null,可通过??设置默认值
    string statusCode = resultElement.Element("statuscode")?.Value;
    string customerId = resultElement.Element("Customer")?.Value;
    string customerName = resultElement.Element("CustomerName")?.Value;
    string customerFullName = resultElement.Element("CustomerFullName")?.Value;
    string fiscalAddress = resultElement.Element("FiscalAddress")?.Value ?? string.Empty;
    string vatRegistration = resultElement.Element("VATRegistration")?.Value ?? string.Empty;

    // 示例输出
    Console.WriteLine($"状态码: {statusCode}");
    Console.WriteLine($"客户ID: {customerId}");
    Console.WriteLine($"客户名称: {customerName}");
    Console.WriteLine($"客户全称: {customerFullName}");
    Console.WriteLine($"财务地址: {fiscalAddress}");
    Console.WriteLine($"VAT注册信息: {vatRegistration}");
}

关键说明:

  • 使用XNamespace表示SOAP命名空间,查询soap:Body时需要将命名空间和元素名拼接(soapNs + "Body")。
  • 通过Descendants找到所有soap:Body元素,再通过Elements("Result")获取其下的Result子元素,最后用FirstOrDefault取第一个匹配的元素。
  • 直接通过Element("元素名")获取子元素,Value属性返回元素文本内容,空元素会返回null,可使用??运算符设置默认值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 04:10:32