當我調用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!我錯過了什麼 ?
你所定義的LoginResponseType類的屬性?如果不是,那麼它將無法將xml中的值反序列化到對象。 – Popo
是的所有屬性都已定義,因爲LoginResponseType是將WSDL引用爲服務時生成的類。當我使用Web服務時,LoginResponseType從網絡接收到的SOAP響應中被反序列化。我想從文件中做同樣的BU。 – abreneliere
什麼是*來自wcf客戶端web服務調用的響應*?你的意思是一個WCF服務響應? –