Building a Soap Message

  • Thread starter Christopher D. Wiederspan
  • Start date
C

Christopher D. Wiederspan

I have a simple project in which I need to build a Soap message "by hand".
Essentially I'm using a System.Net.HttpWebRequest to post data to a
webservice. Everything is working well, but I'm currently using a
StringBuilder to build the Content (the Soap message) portion of the post.
I've got to believe that there is a better way to accomplish this. The
message that I need to build is something like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Execute xmlns="http://tempuri.org/">
<message>string</message>
</Execute>
</soap:Body>
</soap:Envelope>

Is my best bet to build an XmlDocument object that happens to contain the
Soap namespace, or is there some sort of object somewhere else in the
framework that makes building the Soap message easier?

Please let me know if you have any ideas.

Thanks,
Chris
 
S

Steven Cheng[MSFT]

Hi Chris,


Thanks for posting in the community!
From your description, you used the HttpWebRequest component to manually
post a soapMessage xml document and the xml document is generated via
stringbuilder. So currently you're looking for another simpler means to
generate the document, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, I think the XmlSerialization function of the dotnet
framework is what you need. The dotnet xml serialization support serialize
a certain classs instance into a xml document and also deserialize a xml
document into a certain class's instance. For detailed info on Xml
SErialzation using dotnet XML components, you may view the following web
referece:
#Introducing XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconintroducingxmlseri
alization.asp?frame=true

And as for the problem in this issue, you may try defining a certain class
whose instance can be serialized into file as the format you like, such as:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Execute xmlns="http://tempuri.org/">
<message>string</message>
</Execute>
</soap:Body>
</soap:Envelope>

you mentioned in the question. To make the defining task easier, the dotnet
framework also provide a tool named "XML Schema Definition Tool"(xsd.exe)
which can help use generate a dotnet class from a Xml Schema(.xsd) file.

#XML Schema Definition Tool (Xsd.exe)
http://msdn.microsoft.com/library/en-us/cptools/html/cpconXMLSchemaDefinitio
nToolXsdexe.asp?frame=true

And it also can help generate a Xml Schema file from a Xml Document. So as
for the above style document, we can generate a certain class via the
following steps:
1. create a xml file which contains the following content as:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Execute xmlns="http://tempuri.org/">
<message>string</message>
</Execute>
</soap:Body>
</soap:Envelope>
# I named it as message.xml

2. Open the Visual studio dotnet commandline promp window and went to the
folder which contains the "message.xml" file. Then type the below command
to generate a proper Schema(.xsd) file according to the source.xml:
-----------------------------
driver:directoy> xsd message.xml
----------------------------

After that, we'll get two xsd files: message.xsd and message_app1.xsd ,
that is Schema files generated by the xsd.exe

3. Type the below command to generate the class file from the xsd file:
-----------------------------
driver:directoy> xsd message.xsd message_app1.xsd
----------------------------

Well, we'll get a class file named "message_message_app1.cs" in which
contains the certain class's source code, here is the source:
----------------------------------
using System.Xml.Serialization;

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.xmlsoap
org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://schemas.xmlsoap
org/soap/envelope/", IsNullable=false)]
public class Envelope {

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("soap:Body")]
public EnvelopeBody[] Items;
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.xmlsoap
org/soap/envelope/")]
public class EnvelopeBody {

/// <remarks/>

[System.Xml.Serialization.XmlElementAttribute(Namespace="http://tempuri.org/
")]
public Execute Execute;
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/",
IsNullable=false)]
public class Execute {

/// <remarks/>
public string message;
}

-----------------------------------


Then we can use the above class generate xml document using the
XmlSerializer, for example:

XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
Envelope env = new Envelope();
EnvelopeBody eb = new EnvelopeBody();
Execute ext = new Execute();
ext.message = "Hello World!";

eb.Execute = ext;
env.Items = new EnvelopeBody[]{eb};

TextWriter writer = new StreamWriter("output.xml");
serializer.Serialize(writer,env);
writer.Close();

The above code will write the following content to "output.xml":
-----------------------------output.xml---------------
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<soap_x003A_Body>
<Execute xmlns="http://tempuri.org/">
<message>Hello World!</message>
</Execute>
</soap_x003A_Body>
</Envelope>
------------------------------
And to modify the serialiation result exactly as what you want, you need to
apply more Xml Serializaion attributes on the class, for more detailed info
on Xml Serializaion attributes, you may view the following reference in
MSDN:
#Attributes That Control XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconattributesthatcont
rolserialization.asp?frame=true

As for your situation, you may use a memorystream to contain the xmlcontent
and convert int a string. Do you think so?

In addtion, here are some other tech references which may also be helpful
to you:
#Generating SOAP Messages With XML Serialization
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconxmlserializationus
ingsoapprotocol.asp?frame=true

#XML Serialization with XML Web Services
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconxmlserializationwi
thwebservices.asp?frame=true


Please check out my suggestions. If you need any further assistance, please
feel free to post here.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

You can build soap messages using the .NET XML classes, which is a more
attractive method to build it, as you will be certain to have the correct
formatting when you are finished. Create the SOAP message as an XML doc and
add nodes to build the entire envelope hierarchy.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************************************************
Think outside the box!
***************************************************************
 
S

Steven Cheng[MSFT]

Hi Chris,


Have you had a chance to try out my suggestion or have you got any
progresses on this issue? If you have any questions or need any assistance,
please feel free to post here.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
A

Ads

Could you post a sample piece of code. I searched quite a bit and
couldnt get hold of any. I need to send a digitally signed SOAP message
to a weblogic webservice from a vb.net application. If any one could
post or point me in the right direction, it is much appreciated.

thanks
 
D

Dan Rogers

Hi,

Download the WSE 2.0 SP1 toolkit from MSDN. There are samples there that
do exactly what you are looking for.

Regards

Dan Rogers
Microsoft Corporation
--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top