【问题标题】:Construct SOAP Envelope XML using C#使用 C# 构造 SOAP 信封 XML
【发布时间】:2012-12-08 16:03:38
【问题描述】:

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

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

【问题讨论】:

    标签: c# xml soap


    【解决方案1】:

    这是用于调用一些 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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        • 2013-06-02
        相关资源
        最近更新 更多