Solved

Need to remove Soap envelope to parse XML response

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

10 Experts available now in Live!

Get 1:1 Help Now