title: Parse SOAP web service XML response returned from server
body: <p>I need to parsing XML response returned when I call a SOAP web service. The following code works if I pass a normal XML string or document to the <code>xDocument.Load()</code> method, but do not work when I pass in the response XML.</p>
  
  <pre><code>public static HttpWebRequest CreateWebRequest()
          {
              HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http:localhost:15233/myservice.asmx");   
              webRequest.ContentType = "text/xml;charset=\"utf-8\"";
              webRequest.Accept = "text/xml";
              webRequest.Method = "POST";
  
              return webRequest;
          }
  
  public void execute()
  {
  
                HttpWebRequest request = CreateWebRequest();
  
                 XmlDocument soapEnvelopeXml = new XmlDocument();
                 soapEnvelopeXml.LoadXml(@"&lt;?xml version='1.0' encoding='utf-8'?&gt;
     &lt;soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'&gt;
       &lt;soap:Body&gt;
         &lt;Member xmlns='http://tempuri.org/'&gt;
           &lt;memdid&gt;123&lt;/memid&gt;
           &lt;membername&gt;Michael&lt;/membername&gt;
           &lt;location&gt;Somewhere&lt;/location&gt;        
         &lt;/Member&gt;
       &lt;/soap:Body&gt;
     &lt;/soap:Envelope&gt;");
  
       using (Stream stream = request.GetRequestStream())
              {
                  soapEnvelopeXml.Save(stream);
              }
  
  
              //Get the Response    
              HttpWebResponse wr = (HttpWebResponse)request.GetResponse();
              StreamReader rd = new StreamReader(wr.GetResponseStream());
  
             //The response to parse
             string xmlresponse = rd.ReadToEnd();
  
  
                 StringReader sr = new StringReader(xmlresponse);
                 XDocument doc = XDocument.Load(sr);
  
                 //Response.Write(doc);
                 var items = from p in doc.Descendants("MemResponse")
                             select new
                             {
                                 Phone = p.Element("Phone").Value,
                                 Email = p.Element("Email").Value
                             };
                 foreach (var re in items)
                 {
                     Response.Write(re.Phone);
  
                 }
  
  
  }
  </code></pre>
  
  <p>The xml Response returned is of this Format:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt; 
  &lt;MemResponse&gt; 
  &lt;Phone&gt;2554535&lt;/Phone&gt; 
  &lt;Email&gt;mail@mail&lt;/Email&gt;
  &lt;UniqueNumber&gt;we75546654&lt;/UniqueNumber&gt;
   &lt;/MemResponse&gt;
  </code></pre>
  
  <p>and the Response is represented in the code as: string xmlresponse</p>