DataContractSerializer and deserialization of web service response types

I use web service calls and using the WCF service link on the client. I saved the XML responses received from the test service to disk (without SOAP envelope and body tags). I would like to load them from disk and create objects from them. Lets use the following method from my web service:

SomeMethodResponse SomeMethod(SomeMethodRequest req)

I manually (via SOAP UI) save the response to the disk in a file, Example response:

<SomeMethodResponse xmlns="http://myNamespace">
    <SomeMember1>value</SomeMember1>
</SomeMethodResponse>

Then I try to deserialize the object from the file using:

DataContractSerializer dcs = 
  new DataContractSerializer(typeof(SomeMethodResponse))

This fails - the serializer complains about the error that it expects an element in the namespace http://schemas.datacontract.org/2004/07 ', but found the element in' http: // myNamespace '.

: DataContractSerializer , SomeMethodResponseType XmlTypeAttribute(Namespace="http://myNamespace")?

, DataContractSerializer. , :

X Y ( XML-). 'EndElement' 'SomeMethodResponse
'httpmyNapespace . "someNameField".

SomeName - XSD, -. SomeMethodResponse, someNameField. , DataContractSerializer .

XML, , , SomeMethod?

, Matra

+3
2

: mesasge , svcUtil XmlSerializer insted DataContractSerializer. , XmlTypeAttribute - MSDn XML-, , XmlSerializer.;-) , , XmlSerializer

+5

XML- XML:

<SomeMethodResponse xmlns="http://myNamespace">
    <SomeMember1>value</SomeMember1>
</SomeMethodResponse xmlns="http://myNamespace">

, , DataContractSerializer :

DataContractSerializer dcs = 
    new DataContractSerializer(typeof(SomeMethodResponse),
                               "SomeMethodResponse", "http://myNamespace")

XML , XML.

, :

X Y ( XML-). 'EndElement' 'SomeMethodResponse from namespace' httpmyNapespace . "someNameField".

, ........

</SomeMethodResponse xmlns="http://myNamespace">

, !

</SomeMethodResponse>
+5

Source: https://habr.com/ru/post/1737447/


All Articles