title: Parse a soap XML to a C# class
body: <p>I'm trying to parse a SOAP message into a specific Class, but I'm having trouble.</p>
  
  <p>This is the SOAP Message:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;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"&gt;
      &lt;soap:Body&gt;
          &lt;LoginResponse
              xmlns="http://schemas.microsoft.com/sharepoint/soap/"&gt;
              &lt;LoginResult&gt;
                  &lt;CookieName&gt;FedAuth&lt;/CookieName&gt;
                  &lt;ErrorCode&gt;NoError&lt;/ErrorCode&gt;
                  &lt;TimeoutSeconds&gt;1800&lt;/TimeoutSeconds&gt;
              &lt;/LoginResult&gt;
          &lt;/LoginResponse&gt;
      &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>I have a simple class with 3 attributes:</p>
  
  <pre><code>public class SoapResponse
  {
      public string CookieName { get; set; }
  
      public int TimeoutSeconds { get; set; }
  
      public string ErrorCode { get; set; }
  }
  </code></pre>
  
  <p>I'm trying to Use Linq to evaluate the Soap XML and parse it into a an object of the SoapResponse Class. So far I have the next code:</p>
  
  <pre><code>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
      };
  </code></pre>
  
  <p>I know that this is a very similar post to this <a href="https://stackoverflow.com/questions/4329825/using-linq-to-xml-to-parse-a-soap-message">Using LINQ to XML to Parse a SOAP message</a> post, but I can't find a way to work it around.</p>
  
  <p>Thanks in advance! :)</p>