Solved

Need to remove Soap envelope to parse XML response

Posted on 2010-08-20
2
4,191 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now