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
- 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>";
- HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/StudentResults/Results.asmx");
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(soapMessage);
- req.ContentType = "text/xml; charset=utf-8";
- req.Accept = "text/xml";
- req.Method = "POST";
- req.Headers.Add("SOAPAction", @"http://tempuri.org/StudentResults/validateStudent");
- Stream stm = req.GetRequestStream();
- doc.Save(stm);
- stm.Close();
- WebResponse resp = req.GetResponse();
- stm = resp.GetResponseStream();
- StreamReader r = new StreamReader(stm);
- XmlDocument response = new XmlDocument();
- response.LoadXml(r.ReadToEnd());
Expand|Select|Wrap|Line Numbers
- <?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>
- <validateStudentResponse xmlns="http://tempuri.org/StudentResults/">
- <validateStudentResult>
- <validateStudent xmlns="">
- <studentID>1234</studentID>
- <studentName>Andrew Harris</studentName>
- </validateStudent>
- </validateStudentResult>
- </validateStudentResponse>
- </soap:Body>
- </soap:Envelope>
Expand|Select|Wrap|Line Numbers
- XmlNamespaceManager nsm = new XmlNamespaceManager(response.NameTable);
- nsm.AddNamespace("soap","http://schemas.xmlsoap.org/soap/envelope/");
- nsm.AddNamespace("sr","http://tempuri.org/StudentResults/");
- XmlNode currentNode = response.DocumentElement.FirstChild;
- XmlNode name = currentNode.SelectSingleNode("/soap:Envelope/soap:Body/sr:validateStudentResponse/validateStudentResult/validateStudent/studentID", nsm);
Regards
Andrew