none
How to deserialize a SOAP message from the disk ? RRS feed

  • Question

  • Hello,

    I want to deserialize a web service response from an XML that is in a file. When I call the method on the web service, the response is deserialized in the type of the generated WCF proxy client. I want to do the same but from a SOAP message loaded from a file.

    The SOAP message received from the network is the same message as the one located in on the disk:

    <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:LoginResponse>			<return>				<ResultCode>0</ResultCode>				<ResultMessage>Login et password correct.</ResultMessage>				<eur>					<Id>IMT_706</Id>					<Nom>IMA PROTECT</Nom>					<Prenom/>					<nbFI>0</nbFI>					<Fonctioneur>TS</Fonctioneur>					<Timeout>30</Timeout>				</eur>				<ZneGeoList xsi:nil="true"/>			</return>		</ns2:LoginResponse>	</SOAP-ENV:Body></SOAP-ENV:Envelope>

    The class generated by the proxy is :

    [System.Diagnostics.DebuggerStepThroughAttribute()][System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")][System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)][System.ServiceModel.MessageContrAttribute(WrapperName = "LoginResponse", WrapperNamespace = "urn:PlotiIntf-IPloti", IsWrapped = true)]public partial class LoginResponse{	[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "", Order = 0)]	public Ploti.PlotiClient.LoginResponseType @return;	public LoginResponse()	{	}	public LoginResponse(Ploti.PlotiClient.LoginResponseType @return)	{		this.@return = @return;	}}

    I tried serveral ways to parse the message:

    GetCRResponse body;

    using (var xmlReader = XmlReader.Create("Requests\\LoginResponse.xml")) {

    Message m = Message.CreateMessage(xmlReader, int.MaxValue, MessageVersion.Soap11);

    body = m.GetBody<LoginResponse>();

    }

    In the GeyBody method this exception is raised:

    Expected element 'GetCRResponse' from namespace 'http://schemas.datacontr.org/2004/07/Pourtu.PourtuClient'.. Detecting 'Element' with name 'GetCRResponse', namespace 'urn:PourtuIntf-IPourtu'.

    I tried using the SoapFormatter:

    using ( FileStream fs = new FileStream("Requests\\LoginResponse.xml", FileMode.Open) )

    {

        SoapFormatter formatter = new SoapFormatter();

        body = (LoginResponse)formatter.Deserialize(fs);

    }

    ..the Deserialize throws the following exception: Analysis error, no assembly associated to the xml key 'ns2 LoginResponse'.

    And finally I used directly the DataConstr Serializer:

    LoginResponse body;

    FileStream stream = new FileStream("Requests\\LoginResponse.xml", FileMode.Open);

    XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());

    Message m = Message.CreateMessage(xdr, int.MaxValue, MessageVersion.Soap11);

    var xmlReader = m.GetReaderAtBodyContents();

    DataContrSerializer dataContr = new DataContrSerializer(typeof(LoginResponse), "LoginResponse", "urn:PlotiIntf-IPloti",

    new List<Type>() { typeof(LoginResponse), typeof(LoginResponseType), typeof(eurType), typeof(ZoneGeoType) }, Int32.MaxValue, true, true, null);

    var obj = dataContr.ReadObject(xmlReader, false);

    ReadObject does return a LoginResponse instance, but the content LoginResponse.return is allways null.

    So, I'm asking: how does the client proxy deserialize the xml to a LoginResponse object type ?

    Tuesday, May 2, 2017 1:16 PM

All replies

  • Hello,

    I want to deserialize a web service response from an XML that is in a file. When I call the method on the web service, the response is deserialized in the type of the generated WCF proxy client. I want to do the same but from a SOAP message loaded from a file.

    The SOAP message received from the network is the same message as the one located in on the disk:

    <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:LoginResponse>			<return>				<ResultCode>0</ResultCode>				<ResultMessage>Login et password correct.</ResultMessage>				<eur>					<Id>IMT_706</Id>					<Nom>IMA PROTECT</Nom>					<Prenom/>					<nbFI>0</nbFI>					<Fonctioneur>TS</Fonctioneur>					<Timeout>30</Timeout>				</eur>				<ZneGeoList xsi:nil="true"/>			</return>		</ns2:LoginResponse>	</SOAP-ENV:Body></SOAP-ENV:Envelope>

    The class generated by the proxy is :

    [System.Diagnostics.DebuggerStepThroughAttribute()][System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")][System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)][System.ServiceModel.MessageContrAttribute(WrapperName = "LoginResponse", WrapperNamespace = "urn:PlotiIntf-IPloti", IsWrapped = true)]public partial class LoginResponse{	[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "", Order = 0)]	public Ploti.PlotiClient.LoginResponseType @return;	public LoginResponse()	{	}	public LoginResponse(Ploti.PlotiClient.LoginResponseType @return)	{		this.@return = @return;	}}

    I tried serveral ways to parse the message:

    	GetCRResponse body;	using (var xmlReader = XmlReader.Create("Requests\\LoginResponse.xml"))	{		Message m = Message.CreateMessage(xmlReader, int.MaxValue, MessageVersion.Soap11);		body = m.GetBody<LoginResponse>();	}

    In the GeyBody method this exception is raised:

    Expected element 'GetCRResponse' from namespace 'http://schemas.datacontr.org/2004/07/Pourtu.PourtuClient'.. Detecting 'Element' with name 'GetCRResponse', namespace 'urn:PourtuIntf-IPourtu'.

    I tried using the SoapFormatter:

    using ( FileStream fs = new FileStream("Requests\\LoginResponse.xml", FileMode.Open) ){    SoapFormatter formatter = new SoapFormatter();    body = (LoginResponse)formatter.Deserialize(fs);}

    ..the Deserialize throws the following exception: Analysis error, no assembly associated to the xml key 'ns2 LoginResponse'.

    And finally I used directly the DataConstr Serializer:

    LoginResponse body;FileStream stream = new FileStream("Requests\\LoginResponse.xml", FileMode.Open);XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas());Message m = Message.CreateMessage(xdr, int.MaxValue, MessageVersion.Soap11);var xmlReader = m.GetReaderAtBodyContents();DataContrSerializer dataContr = new DataContrSerializer(typeof(LoginResponse), "LoginResponse", "urn:PlotiIntf-IPloti",	new List<Type>() { typeof(LoginResponse), typeof(LoginResponseType), typeof(eurType), typeof(ZoneGeoType) }, Int32.MaxValue, true, true, null);var obj = dataContr.ReadObject(xmlReader, false);

    ReadObject does return a LoginResponse instance, but the content LoginResponse.return is allways null.

    So, I'm asking: how does the client proxy deserialize the xml to a LoginResponse object type ?

    Tuesday, May 2, 2017 1:12 PM
  • What is the definition of LoginResponse, LoginResponseType and etc?

    I tried to make a test with your last code, but it is not complete.

    It would be helpful if you could share us the complete code.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 4, 2017 6:24 AM