Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct SOAP Envelope XML using C#

Tags:

c#

soap

xml

I searched all over and i dont know what is the best way to construct an XML like this in C#.

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
        <SOAP-ENV:Body>
            <ns5572:calculate_something xmlns:ns5572="http://tempuri.org">
                <Input_data>
                <code_user xsi:type="xsd:string">test_user</code_user>
                <password_broker xsi:type="xsd:string">test_password</password>
                <subuser_id xsi:type="xsd:string"></subuser_id>
                </Input_data>
            </ns5572:calculate_something>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

My question is if there are any special dedicated classes for this kind of structure. Thanks in advance.

like image 804
Alex Avatar asked Apr 08 '26 22:04

Alex


1 Answers

This is the XML for calling some SOAP web service, to call this via C# you can add it as a service reference to your C# project.

You need the link to the WSDL (Web service definition language file) of your service, then you can add the service reference to your project and call any of its functionality easily in this way:

1- Define a client to use it to call the service:

MyTestServiceSoapClient client = new MyTestServiceSoapClient();

2- Call some method of this client this way:

client.calculate_something("test_user", "test_password", "");

or this:

client.calculate_something(new Input_data() 
{ code_user  = "test_user", password_broker  = "test_password", subuser_id  = ""}
);

This article will help you to add the service reference to your C# project.

To intercept and XMLs of the request and the response, Implement these two classes:

public class InspectorBehavior : IEndpointBehavior
{
    public string LastRequestXML { 
        get
        {
            return myMessageInspector.LastRequestXML;
        }
    }

    public string LastResponseXML { 
        get
        {
            return myMessageInspector.LastResponseXML;
        }
    }


    private MyMessageInspector myMessageInspector = new MyMessageInspector();
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {

    }

    public void Validate(ServiceEndpoint endpoint)
    {

    }


    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(myMessageInspector );
    }
}





public class MyMessageInspector : IClientMessageInspector
{
    public string LastRequestXML { get; private set; }
    public string LastResponseXML { get; private set; }
    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
        LastResponseXML = reply.ToString();
    }

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
    {
        LastRequestXML = request.ToString();
        return request;
    }
}

Then, change the call code to:

MyTestServiceSoapClient client = new MyTestServiceSoapClient();
var requestInterceptor = new InspectorBehavior();
client.Endpoint.Behaviors.Add(requestInterceptor );
client.calculate_something("test_user", "test_password", "");
string requestXML = requestInterceptor.LastRequestXML;
string responseXML = requestInterceptor.LastResponseXML;
// Now the xml you need is in "requestXML" variable
like image 157
Sawan Avatar answered Apr 11 '26 10:04

Sawan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!