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.)