2017-05-02 51 views
10

當我調用Web服務操作時,WCF使用DataContractSerializer將消息反序列化到代理類:爲什麼我不能這樣做?如何使用DataContractSerializer從文件反序列化WCF soap響應消息?

下面是在文件ActLoginResponse.xml SOAP消息:

<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:PlotiIntf" xmlns:ns2="urn:PlotiIntf-IPloti" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"/> 
    <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
     <ns2:ActLoginResponse> 
      <return> 
       <ResultCode>0</ResultCode> 
       <ResultMessage>Login et password correct.</ResultMessage> 
       <Acteur> 
        <Id>IMT_706</Id> 
        <Nom>IMA PROTECT</Nom> 
        <Prenom/> 
        <nbFI>0</nbFI> 
        <FonctionActeur>TS</FonctionActeur> 
        <Timeout>30</Timeout> 
       </Acteur> 
       <ZneGeoList xsi:nil="true"/> 
      </return> 
     </ns2:ActLoginResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

爲相應ActLoginResponse類的WCF代理代碼是:

[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] 
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] 
[System.ServiceModel.MessageContractAttribute(WrapperName="ActLoginResponse", WrapperNamespace="urn:PlotiIntf-IPloti", IsWrapped=true)] 
public partial class ActLoginResponse { 

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="", Order=0)] 
    public Ploti.PlotiClient.LoginResponseType @return; 

    public ActLoginResponse() { 
    } 

    public ActLoginResponse(Ploti.PlotiClient.LoginResponseType @return) { 
     [email protected] = @return; 
    } 
} 

所以我需要的XML解析到ActLoginResponse類型的對象實例。

這裏是我完成解析:

 ActLoginResponse body; 
     FileStream stream = new FileStream("Requests\\ActLoginResponse.xml", FileMode.Open); 
     XmlReader xmlReader = XmlReader.Create(stream); 

     xmlReader.MoveToContent(); 
     xmlReader.ReadStartElement(); 
     xmlReader.MoveToContent(); 
     xmlReader.ReadStartElement(); 
     xmlReader.MoveToContent(); 
     xmlReader.ReadStartElement(); 
     xmlReader.MoveToContent(); 

     // the the reader is on the element ActLoginResponse (that is confirmed by a Log.Debug(xmlReader.ReadOuterXml()); 

     // I create The DataContractSerializer: exception if nampespace is not specified 
     DataContractSerializer dataContract = new `DataContractSerializer`(typeof(ActLoginResponse), "ActLoginResponse", "urn:PlotiIntf-IPloti"); 

     ActLoginResponse actLogin = dataContract.ReadObject(xmlReader, true); 

的actLogin對象被創建,但內容actLogin.return是百達NULL!我錯過了什麼 ?

+0

你所定義的LoginResponseType類的屬性?如果不是,那麼它將無法將xml中的值反序列化到對象。 – Popo

+0

是的所有屬性都已定義,因爲LoginResponseType是將WSDL引用爲服務時生成的類。當我使用Web服務時,LoginResponseType從網絡接收到的SOAP響應中被反序列化。我想從文件中做同樣的BU。 – abreneliere

+0

什麼是*來自wcf客戶端web服務調用的響應*?你的意思是一個WCF服務響應? –

回答

2

SoapReflectionImporter的幫助下,您應該可以使用XmlSerializer來完成此操作。

var importer = new SoapReflectionImporter("urn:PlotiIntf-IPloti"); 
var mapping = importer.ImportTypeMapping(typeof(ActLoginResponse)); 
var serializer = new XmlSerializer(mapping); 
var response = serializer.Deserialize(reader) as ActLoginResponse; 

的SoapReflectionImporter類提供類型映射到 SOAP編碼消息部分,如在Web服務描述語言 (WSDL)文件來定義。它僅在Web服務或客戶端 指定SOAP編碼時使用,如SOAP 1.1 規範的第5節中所述。

下面的命令是用來從WSDL

SvcUtil.exe IPlotiservice.wsdl /t:code /serviceContract 
3

生成你的客戶合同我用你已經從其它問題提供的WSDL和它創造的代理類。

使用上面的XML,我似乎都沒有問題,反序列化到ActLoginResponse通過如下:

Message m = Message.CreateMessage(XmlReader.Create("C:\\testSvc\\login.xml"), int.MaxValue, MessageVersion.Soap11); 
SoapReflectionImporter importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "urn:PlotiIntf-IPloti"); 
XmlTypeMapping mapp = importer.ImportTypeMapping(typeof(ActLoginResponse)); 
XmlSerializer xmlSerializer = new XmlSerializer(mapp); //typeof(T), xr 
var o = (ActLoginResponse)xmlSerializer.Deserialize(m.GetReaderAtBodyContents()); 

reference

相關問題