2016-06-21 2 views
1

SOAP 메시지를 특정 클래스로 구문 분석하려고하는데 문제가 있습니다.비누 XML을 C# 클래스로 구문 분석합니다.

public class SoapResponse 
{ 
    public string CookieName { get; set; } 

    public int TimeoutSeconds { get; set; } 

    public string ErrorCode { get; set; } 
} 

나는 비누 XML을 평가하기 위해 Linq를 사용하고로 구문 분석하려고 해요 : 나는 3 개 속성을 가진 간단한 클래스가 가지고

<?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> 

:

는 SOAP 메시지입니다 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 
    }; 

I는이 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;} 

     } 




} 
관련 문제