Solved

Need to remove Soap envelope to parse XML response

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Suggested Courses

617 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