DataContractSerializer and deserializing web service response types
Posted
by matra
on Stack Overflow
See other posts from Stack Overflow
or by matra
Published on 2010-03-18T00:09:09Z
Indexed on
2010/03/18
0:11 UTC
Read the original article
Hit count: 1380
Hi,
I am using calling web services and using WCF generated service-reference on the client. I have saved XML responses that are received from test service to disk (without SOAP envelope and body tags) I would like to load them from disk and create objects from them. Lets' take the following method from my web service:
SomeMethodResponse SomeMethod(SomeMethodRequest req)
I manually (through SOAP UI) save the response to disk to file, Sample response:
< SomeMethodResponse xmlns="http://myNamespace">
<SomeMember1>value</SomeMember1>
</SomeMethodResponse xmlns="http://myNamespace">
Then I try to deserialize the object from file using:
DataContractSerializer dcs =
new DataContractSerializer(typeof(SomeMethodResponse))
This fails – the serializer complains with the error, that it is expecting element in namespace 'http://schemas.datacontract.org/2004/07', but found element in 'http://myNamespace'.
Question: Why does the DataContractSerializer
not use the namespace, that is declared on SomeMethodResponseType with XmlTypeAttribute(Namespace="http://myNamespace")
?
I can work around this by explicitly providing the namespace and the root element to DataContractSerializer
constructor. But then it fails with message similar to:
Error in line X position Y (last line of the XMLdocument). 'EndElement' 'SomeMethodResponse
from namespace 'httpmyNapespace’ is not expected.
Expecting element 'someNameField'.
SomeName
is an element in the XSD that web service is using. It is also a property on the SomeMethodResponse
type, backed by the private field called someNameField
. It looks like DataContractSerializer
is trying to deserialize the fields in addition to properties.
How can I deserailize XML that I have saved from disk and get back the object of same type that SomeMethod is returning?
Thanks, Matra
© Stack Overflow or respective owner