2016-06-21 3 views
1

SOAPメッセージを特定のクラスに解析しようとしていますが、問題があります。SOAPを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 
    }; 

を私がこの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;} 

     } 




} 
関連する問題