使用C#构造SOAP Envelope XML

时间:2012-12-08 16:03:38

标签: c# xml soap

我搜遍了全部,我不知道在C#中构建这样的XML的最佳方法是什么。

    <?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>

我的问题是这种结构是否有任何特殊的专用类。 提前谢谢。

2 个答案:

答案 0 :(得分:3)

这不是一些随机的XML。它看起来像一个SOAP请求。查看this

答案 1 :(得分:2)

这是用于调用某些SOAP Web服务的XML,要通过C#调用它,您可以将其作为服务引用添加到C#项目中。

您需要指向服务的WSDL(Web服务定义语言文件)的链接,然后您可以将服务引用添加到项目中并以这种方式轻松调用其任何功能:

1-定义客户端以使用它来调用服务:

MyTestServiceSoapClient client = new MyTestServiceSoapClient();

2-以这种方式调用此客户端的某些方法:

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

或者这个:

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

This article将帮助您将服务引用添加到C#项目中。

要拦截请求和响应的XML,请实现以下两个类:

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;
    }
}

然后,将呼叫代码更改为:

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