Use XmlSerializer on request and DataContractSerializer on response?
Is it possible to receive a request with attributes and use the XmlSerializer to deserialize it and send a response back with just elements using the DataContractSeriali开发者_StackOverflow中文版zer?
Also, if you receive a request with attributes, must you use the XmlSerializer to deserialize the content?
For the second question: if you have attributes, then you need to use the XmlSerializer - the DataContractSerializer doesn't support them.
For the first question: yes, it's possible. No, it's not easy. The selection of the serializer is done at the operation formatter level. WCF allows you to change the serializer per operation by using the [XmlSerializerFormat] or [DataContractFormat] (which is default), but that will bind the serializer to both request and responses.
If you really, really want to do it, you can create your own formatter (replace it using some operation behavior), then in that formatter you can choose how you serialize / deserialize the inputs / outputs (you can even use different serializers for each parameter), but you'll need to create one formatter, which isn't something too easy to do.
I've written some posts about message formatters and replacing serializers in Silverlight which can give you a hint on how to start about it. But if you can live with XmlSerializer only, that'll be a lot easier.
精彩评论