Click here to Skip to main content
Licence CPOL
First Posted 17 Apr 2007
Views 35,508
Downloads 387
Bookmarked 13 times

Extract Soap Body From Soap Message

By | 17 Apr 2007 | Article
Extract XML Data from Soap Message

Introduction

The static function extracts the Soap Body (XML Data) from a Soap Message.

Using the Code

To extract the soap body, the key point is to get the soap envelope prefix. The soap prefix can be found from the first level Nodes.

For example, the soap message may look like:

<?Xml version="1.-" encoding="utf-8">
<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:header>
        <id>AAA</id>
    </soap:header>
    <soap:body>
        <order>
            <id>1234</id>
        </order>
    </soap:body>
</soap:envelope>

The second element of the message declared the soap Envelope namespace prefix, "Soap".
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/".

System.Xml.XMLNode class provides a function for us to lookup the prefix from the namespace. So we have to locate the xmlnode which has namespace declaration, and lookup the "http://schemas.xmlsoap.org/soap/envelope/" namespace's prefix.

//load the Soap Message
System.Xml.XmlDocument doc=new System.Xml.XmlDocument();
doc.LoadXml(SoapMessage);

//search for the "http://schemas.xmlsoap.org/soap/envelope/" URI prefix
string prefix = "";
for(int i=0; i<doc.ChildNodes.Count;i++)
{
    System.Xml.XmlNode soapNode= doc.ChildNodes[i];
    prefix= soapNode.GetPrefixOfNamespace(http://schemas.xmlsoap.org/soap/envelope/);
    if(prefix!=null&&prefix.Length>0)
        break;
}

After determining the prefix, use the prefix to help us locate soap body (XML Data), start and end index in the soap message, then extract the body substring out.

//find soap body start index
int iSoapBodyElementStartFrom=SoapMessage.IndexOf("<"+ prefix+":Body");
int iSoapBodyElementStartEnd=SoapMessage.IndexOf(">",iSoapBodyElementStartFrom);
iSoapBodyStartIndex=iSoapBodyElementStartEnd+1;    

//find soap body end index
iSoapBodyEndIndex=SoapMessage.IndexOf("</"+prefix+":Body>")-1;
            
//get soap body (XML data)
return SoapMessage.Substring
	(iSoapBodyStartIndex,iSoapBodyEndIndex-iSoapBodyStartIndex+1);

History

  • 17th April, 2007: First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

BillyLiu



Singapore Singapore

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHi PinmemberSale_08221:02 23 Apr '12  
Questionextract soap envelope structure and messages using WSDL url PinmemberMember 42688111:39 24 Jul '09  
General- Extract Soap Body From Soap Message Pinmembervenkat17521:02 7 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 17 Apr 2007
Article Copyright 2007 by BillyLiu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid