【发布时间】:2016-06-21 16:06:36
【问题描述】:
我正在尝试将 SOAP 消息解析为特定的类,但遇到了问题。
这是 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://schemas.microsoft.com/sharepoint/soap/">
<LoginResult>
<CookieName>FedAuth</CookieName>
<ErrorCode>NoError</ErrorCode>
<TimeoutSeconds>1800</TimeoutSeconds>
</LoginResult>
</LoginResponse>
</soap:Body>
</soap:Envelope>
我有一个具有 3 个属性的简单类:
public class SoapResponse
{
public string CookieName { get; set; }
public int TimeoutSeconds { get; set; }
public string ErrorCode { get; set; }
}
我正在尝试使用 Linq 评估 Soap XML 并将其解析为 SoapResponse 类的对象。到目前为止,我有下一个代码:
var xml = XDocument.Parse(responseXml);
var soapResponse = from result in xml.Descendants(XName.Get("LoginResult", xmlNamespace))
let cookieNameElement = result.Element(XName.Get("CookieName", xmlNamespace)) where cookieNameElement != null
let timoutSecondsElement = result.Element(XName.Get("TimoutSeconds", xmlNamespace)) where timoutSecondsElement != null
let errorCodeElement = result.Element(XName.Get("ErrorCode", xmlNamespace)) where errorCodeElement != null
select new SoapResponse
{
CookieName = cookieNameElement.Value,
TimeoutSeconds = Convert.ToInt32(timoutSecondsElement.Value),
ErrorCode = errorCodeElement.Value
};
我知道这是与Using LINQ to XML to Parse a SOAP message 帖子非常相似的帖子,但我找不到解决方法。
提前致谢! :)
【问题讨论】: