0

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 xDocument.Load() method, but do not work when I pass in the response XML.

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(@"<?xml version='1.0' encoding='utf-8'?>
   <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/'>
     <soap:Body>
       <Member xmlns='http://tempuri.org/'>
         <memdid>123</memid>
         <membername>Michael</membername>
         <location>Somewhere</location>        
       </Member>
     </soap:Body>
   </soap:Envelope>");

     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);

               }


}

The xml Response returned is of this Format:

    <?xml version="1.0" encoding="UTF-8"?> 
<MemResponse> 
<Phone>2554535</Phone> 
<Email>mail@mail</Email>
<UniqueNumber>we75546654</UniqueNumber>
 </MemResponse>

and the Response is represented in the code as: string xmlresponse

  • You need to account for the namespaces in the SOAP response. Take a look at this [answer](http://stackoverflow.com/a/2340497/745969) for an example – Tim Mar 07 '17 at 21:25
  • If you want to work with webservice s, you might want to take a look at WCF – LeonG Mar 07 '17 at 21:27
  • You can also use LocalName of elements instead of using namespaces which is demonstrated in another answer in the same question referenced by Tim. It is generally better to use namespaces if you can though. – Crowcoder Mar 07 '17 at 21:31
  • @user3269519: code and mark-up is best not submitted in comments. If you need someone to see that XML, please edit it into the question and use a formatted block. – halfer Mar 08 '17 at 09:19
  • Thanks @halfer for the Correction. I have Updated the question – user3269519 Mar 08 '17 at 11:33

0 Answers0