如何使用c#获取SOAP返回值

本文关键字:SOAP 返回值 获取 何使用 | 更新日期: 2023-09-27 18:24:58

我有一个SOAP响应,如下

<SOAP-ENV:Envelope xmlns:SOAP-ENV='"http://schemas.xmlsoap.org/soap/envelope/'">
<SOAP-ENV:Header/><SOAP-ENV:Body>
<Login xmlns='"http://s.com/sch">   
<error/>
<userid>11</userid>
</Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我正在使用XDocument来解析此SOAP。我正在尝试获得userid 的值

我的代码是

XDocument xDocc = XDocument.Parse(responseSOAP);
string uid = xDocc.Descendants("userid").First().Value;

我无法获得11的值,它只是返回给我一个NULL

如何使用c#获取SOAP返回值

这应该有效:

XDocument xDocc = XDocument.Parse(responseSOAP);
XmlReader xr = xDocc.CreateReader();
xr.ReadToFollowing("userid");
string uid = xr.ReadElementString();

试试这个:

var result = from p in xDocc.Descendants() 
             where p.Name.LocalName == "userid" select p.Value;