Create SOAP envelope in c#.net

QuestionsCategory: C#Create SOAP envelope in c#.net
XorientXorient asked 5 months ago

How to Create SOAP envelope in c#.net?

1 Answers
Best Answer
Mahesh DeshmaneMahesh Deshmane answered 5 months ago

To get the data from service via SOAP, SOAP envelope should contains one Header element and one Body elements .
Some case it also needs encoding style but it’s optional
WSDL  provides more information about what are prerequisite like encoding styles or attributes to call the service.
Here is C# code snippet below which creates SOAP envelope.


private static XmlDocument CreateSoapEnvelope()
{
    XmlDocument soapEnvelopeXml = new XmlDocument();
    soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
            <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
            < soap:Body >
            < WebMethod1 xmlns = ""http://www.dummyservice.com/path/"">
            < Parameter1 > param1 </ Parameter1 >
            < Parameter2 > param2 </ Parameter2 >
            < Parameter3 > param3 </ Parameter3 >
            </ Method >            </ soap:Body >
            </ soap:Envelope > ");
     return soapEnvelopeXml;
}

Note: make sure to change the method name, service name and parameters in Body elements from above code snippet before use.