Using XMLSerializer to control message serialization

By default, WCF runtime uses DataContractSerializer to perform data serialization and deserialization. However, in some cases, we will prefer using XMLSerializer, which will give developers more control over the serialized XML content or will work more closely with some POX clients (like ASMX Web Service client).

How to do it...

  1. First, we should make our data type ready for XMLSerializer. This can be done by adding XML serialization attributes on our data types. The following User class has been decorated with several XML serialization attributes (XmlRootAttribute for top-level type and XmlElementAttribute for type members).
    [XmlRoot(ElementName="UserObject",Namespace="http://wcftest.org/xmlserializer")] ...

Get Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.