Solved

Need to remove Soap envelope to parse XML response

Posted on 2010-08-20
2
4,332 Views
Last Modified: 2012-05-10
I am using asp.net 2.0 and a webservice that returns XML data as a string.
I am able successfully load the xml with XMLDocument.LoadXML(strXML). The response is inside a soap envelope in a plain text format.

How do I access the xml values ('LoginResponse' and SECTOKEN) as XML nodes without resorting to string manipulation (i.e. chopping off the soap enveleope, or using instr(SECTOKEN) to find the value).

That is, how do I remove the soap and get to the good stuff?

Example XML response below.

Thanks!
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:iws0005lResponse xmlns:ns="http://login.wsbeans.iseries/xsd"><ns:return><ns:_LS_OUTPUT_PARM_BUFF_STRUCT><ns:_LS_OUTPUT_PARM_BUFF>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&#xD;
&lt;LoginResponse result="SUCCESS"&gt;&#xD;
&lt;SECTOKEN&gt;4BC952DED0D2BA39A714F3BEB2A8787D71041A02F60A1C5527807B&lt;/SECTOKEN&gt;&#xD;
&lt;/LoginResponse&gt;&#xD;
</ns:_LS_OUTPUT_PARM_BUFF><ns:_LS_OUTPUT_PARM_BUFF></ns:_LS_OUTPUT_PARM_BUFF><ns:_LS_OUTPUT_PARM_BUFF></ns:_LS_OUTPUT_PARM_BUFF><ns:_LS_OUTPUT_PARM_BUFF></ns:_LS_OUTPUT_PARM_BUFF></ns:_LS_OUTPUT_PARM_BUFF_STRUCT></ns:return></ns:iws0005lResponse></soapenv:Body></soapenv:Envelope>

Open in new window

0
Comment
Question by:NELIXINC
2 Comments
 
LVL 4

Accepted Solution

by:
yesthatbob earned 500 total points
ID: 33489817
You can locate the node containing the response using XPath passed to XmlDocument.SelectSingleNode(), and then refer to its InnerText property to get the formatted XML. Then, you can read this formatted XML and use XPath again to get to the actual result value. See my code below.
// Assumes you have your SOAP envelope already loaded into an XmlDocument named 'doc'
			XmlDocument doc = new XmlDocument();
			doc.Load(...);

			// Specify namespace manager to handle 'soapenv' and 'ns' namespaces
			NameTable nameTbl = new NameTable();
			XmlNamespaceManager nsMgr = new XmlNamespaceManager(nameTbl);
			nsMgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
			nsMgr.AddNamespace("ns", "http://login.wsbeans.iseries/xsd");
			
			// Find the response element using XPath
			XmlNode outputElem = doc.SelectSingleNode("/soapenv:Envelope/soapenv:Body/ns:iws0005lResponse/ns:return/ns:_LS_OUTPUT_PARM_BUFF_STRUCT/ns:_LS_OUTPUT_PARM_BUFF", nsMgr);

			// Get response and remove leading white space (since xml declaration can't have leading whitespace)
			string responseText = outputElem.InnerText;
			responseText = responseText.Substring(responseText.IndexOf('<'));

			// responseText will now contain the actual response formatted as XML

			// Load response XML into XMLDocument
			doc.LoadXml(responseText);

			// Find LoginResponse using XPath
			outputElem = doc.SelectSingleNode("/LoginResponse");

			// Get value of result attribute
			string result = outputElem.Attributes["result"].Value;

Open in new window

0
 

Author Closing Comment

by:NELIXINC
ID: 33501883
Thank you so much. You're code works beautifully. Thanks especially for plugging in the namespace values from my example code. That was the part I was missing completely, and now I'm beginning to understand it.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question