【问题标题】:How to read soap response in c#?如何在 C# 中读取肥皂响应?
【发布时间】:2016-02-04 22:31:58
【问题描述】:

Soap 响应为:

<?xml version="1.0" encoding="utf-8" ?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <LoginResponse xmlns="http://example.com/SystemIntegration">
            <FirstName>@FirstName</FirstName>
            <LastName>@LastName</LastName>
        </LoginResponse>
    </soap:Body>
 </soap:Envelope>

我试图将其解读为:

XDocument doc = XDocument.Parse(strReturnStatus);
List<XElement> result = doc.Elements("LoginResponse").ToList();
for (int intc = 0; intc <= result.Count - 1; intc++)
{
    strResponseCode = result[intc].Element("FirstName").Value.ToString();
    strResponseText = result[intc].Element("LastName").Value.ToString();
}

但它返回 null 结果。

如何阅读以上回复asp.net c#??

【问题讨论】:

标签: c# asp.net


【解决方案1】:

最简单的事情是为您的服务创建一个代理类。

您可以使用'Add Service Reference' option in Visual Studio 做到这一点。输入服务的 URL,Visual Studio 会为你生成源代码。

从那时起,您可以使用 C# 代码访问该服务。无需手动提取 SOAP 消息的负载。

如果您是该服务的实施者,请注意:ASMX Web 服务已被弃用很长时间。如果可以,请使用 WCF。

【讨论】:

    【解决方案2】:

    使用 XDocumentobject 中的 Descendants() 方法浏览 XML 节点以获取元素,您可以按照本文Using C# to parse a SOAP Response中提供的步骤进行操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多