3

我到处搜索,我不知道在 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>

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

4

2 回答 2

3

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

于 2012-12-08T16:18:04.850 回答
3

这是用于调用一些 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  = ""}
);

本文将帮助您将服务引用添加到您的 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
于 2012-12-08T16:26:36.887 回答