【发布时间】:2021-10-19 07:38:03
【问题描述】:
我正在从 Web 服务响应接收 SOAP,但由于有多个命名空间,我不确定如何调用它。任何帮助表示赞赏。
回复如下:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<getCompaniesResponse xmlns="http://mastercard.com/sd/pc/service" xmlns:ns2="http://mastercard.com/sd/pc2/service">
<company>
<id>123456</id>
<name>Name is here</name>
<issuerName>Issuer name is here</issuerName>
</company>
<errorMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
</getCompaniesResponse>
</S:Body>
</S:Envelope>
我试图从 SOAP 中获取公司元素。到目前为止,这是我尝试过的:
var xe = XElement.Parse(response);
XNamespace ns = "http://mastercard.com/sd/pc2/service";
var obj = (string)xe.Descendants(ns + "company").Single();
Console.WriteLine(obj);
在上面的代码中,我假设我只需要最后一个命名空间,但它返回 null。我还尝试了其他两个名称空间,结果相同。
【问题讨论】:
标签: c# xml soap namespaces