2016-06-21 68 views
1

我想解析一個SOAP消息到一個特定的類,但我遇到了麻煩。解析一個SOAP XML到一個C#類

這是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帖子,但我不能找到一種方法,圍繞解決它。

在此先感謝! :)

回答

3

嘗試下面的代碼。我從第一個標籤中移除了肥皂。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 
using System.IO; 

namespace ConsoleApplication102 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 
      string responseXml = 
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
       "<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>" + 
       "</Envelope>"; 

      XDocument xml = XDocument.Parse(responseXml); 
      var soapResponse = xml.Descendants().Where(x => x.Name.LocalName == "LoginResult").Select(x => new SoapResponse() 
      { 
       CookieName = (string)x.Element(x.Name.Namespace + "CookieName"), 
       TimeoutSeconds = (int)x.Element(x.Name.Namespace + "TimeoutSeconds"), 
       ErrorCode = (string)x.Element(x.Name.Namespace + "ErrorCode") 
      }).FirstOrDefault(); 

     } 

    } 
     public class SoapResponse 
     { 
      public string CookieName { get; set;} 
      public int TimeoutSeconds { get; set;} 
      public string ErrorCode { get; set;} 

     } 




}