Connecting Tech Pros Worldwide Forums | Help | Site Map

C#-XPath - Help with XPath Query and SOAP Response

Newbie
 
Join Date: May 2007
Posts: 2
#1: May 13 '07
Hello,

I have a C# application that is consuming a C# WebService. I am calling a method on the WebService and it is sending me back a response which contains an XmlDocument:
Expand|Select|Wrap|Line Numbers
  1. string soapMessage = "<?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><validateStudent xmlns=\"http://tempuri.org/StudentResults/\"><studentID>" + txtStudentID.Text + "</studentID></validateStudent></soap:Body></soap:Envelope>";
  2. HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/StudentResults/Results.asmx");
  3. XmlDocument doc = new XmlDocument();
  4.  
  5. doc.LoadXml(soapMessage);
  6.  
  7. req.ContentType = "text/xml; charset=utf-8";
  8. req.Accept = "text/xml";
  9. req.Method = "POST";
  10. req.Headers.Add("SOAPAction", @"http://tempuri.org/StudentResults/validateStudent");
  11. Stream stm = req.GetRequestStream();
  12. doc.Save(stm);
  13. stm.Close();
  14. WebResponse resp = req.GetResponse();
  15. stm = resp.GetResponseStream();
  16. StreamReader r = new StreamReader(stm);
  17.  
  18. XmlDocument response = new XmlDocument();
  19. response.LoadXml(r.ReadToEnd());
  20.  
Problem I have is that the Stream response
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2.   <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">
  3.     <soap:Body>
  4.       <validateStudentResponse xmlns="http://tempuri.org/StudentResults/">
  5.         <validateStudentResult>
  6.           <validateStudent xmlns="">
  7.             <studentID>1234</studentID>
  8.             <studentName>Andrew Harris</studentName>
  9.           </validateStudent>
  10.         </validateStudentResult>
  11.       </validateStudentResponse>
  12.     </soap:Body>
  13. </soap:Envelope>
contains the SOAP envelope and I cant get the following XPath Query to return the relevant Node, the "name" XmlNode is always "Undefined"?

Expand|Select|Wrap|Line Numbers
  1. XmlNamespaceManager nsm = new XmlNamespaceManager(response.NameTable);
  2. nsm.AddNamespace("soap","http://schemas.xmlsoap.org/soap/envelope/");
  3. nsm.AddNamespace("sr","http://tempuri.org/StudentResults/");
  4.  
  5. XmlNode currentNode = response.DocumentElement.FirstChild;
  6. XmlNode name = currentNode.SelectSingleNode("/soap:Envelope/soap:Body/sr:validateStudentResponse/validateStudentResult/validateStudent/studentID", nsm);
Any help appreciated.

Regards

Andrew



Newbie
 
Join Date: May 2007
Posts: 2
#2: May 14 '07

re: C#-XPath - Help with XPath Query and SOAP Response


Anyone able to help?
kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#3: May 14 '07

re: C#-XPath - Help with XPath Query and SOAP Response


Have you tried stripping the soap envelope?
Reply