Is there a way to make DataContractSerializer output cleaner XML? Is there a way to make DataContractSerializer output cleaner XML? xml xml

Is there a way to make DataContractSerializer output cleaner XML?


This is happening because you must have marked your types (e.g. AgentNotification) with [Serializable]. When DataContractSerializer encounters a type marked with [Serializable] but no explicit [DataContract], it generates a default contract for the type that matches how BinaryFormatter serializes a class, which is to serialize all member variables of a class — even variables marked as private — by name. For auto-implemented properties, this means the secret backing fields get serialized by name; their names are the peculiar element names you are seeing.

The easiest way to solve this is to remove the [Serializable] attribute from your classes. You almost certainly don't need it unless you are actually using BinaryFormatter or SoapFormatter. Having done so, DataContractSerializer will now serialize your public properties and fields by name, rather than public and private fields by name.


The long element names (such as, _x003C_Created_x003E_k__BackingField) are created by .NET when you use autoproperties.

If you changed them to properties with backing fields, they would instead use your backing field names. You can do that without adding any attributes to your code.

(Beyond that, simply adding a [DataContract] attribute to your class definition will tidy up your XML a lot -- though not completely.)


The DataContractSerializer will serialize either all public properties (if you don't specify anything - possible as of .NET 3.5 SP1), or (the approach I prefer) anything you label with a [DataMember] attribute.

So the best you can do is mark your class with a [DataContract] attribute, and all the members (properties, fields, etc.) that you really want in your data contract with a [DataMember] attribute.

The DataContractSerializer doesn't really allow much more control than that - you can define quite clearly (using this explicit "opt-in" approach) what gets serialized, but you have little or no control over how it gets serialized.

But do you really need that? REALLY?

If so, you'll probably have to use the XmlSerializer for that serialization process instead - there you can get more control over how things are serialized (but as a drawback, the XmlSerializer will serialize every public property that's not explicitly marked with a [XmlIgnore] attribute - an "opt-out" scheme).

Check out Dan Rigsby's blog post on the differences between DataContractSerializer and XmlSerializer and what each of them has to offer.