?
Solved

Need to remove Soap envelope to parse XML response

Posted on 2010-08-20
2
Medium Priority
?
4,824 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
yesthatbob earned 2000 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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 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