2012-12-08 89 views
2

我搜索了全部,我不知道什麼是在C#中構建這樣的XML的最佳方式。使用C#構建SOAP信封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

這是用於調用某個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 
3

這不是一些隨機的XML。它看起來像一個SOAP請求。看看this