By default, the DataContractSerializer serializes all publicly visible types. All public read/write properties and fields of the type are serialized.
You can change the default behavior by applying the DataContractAttribute and DataMemberAttribute attributes to the types and members This feature can be useful in situations in which you have types that are not under your control and cannot be modified to add attributes. The DataContractSerializer recognizes such "unmarked" types.
You can apply the DataContractAttribute and DataMemberAttribute attributes to explicitly control or customize the serialization of types and members. In addition, you can apply these attributes to private fields. However, even types that are not marked with these attributes are serialized and deserialized. The following rules and exceptions apply:
The DataContractSerializer infers a data contract from types without attributes using the default properties of the newly created types.
All public fields, and properties with public
setmethods are serialized, unless you apply the IgnoreDataMemberAttribute attribute to that member.
The serialization semantics are similar to those of the XmlSerializer.
In unmarked types, only public types with constructors that do not have parameters are serialized. The exception to this rule is ExtensionDataObject used with the IExtensibleDataObject interface.
Read-only fields, properties without a
setmethod, and properties with internal or private
getmethods are not serialized. Such properties are ignored and no exception is thrown, except in the case of get-only collections.
XmlSerializer attributes (such as XmlElement, XmlAttribute, XmlIgnore, XmlInclude, and so on) are ignored.
If you do not apply the DataContractAttribute attribute to a given type, the serializer ignores any member in that type to which the DataMemberAttribute attribute is applied.
The KnownTypes property is supported in types not marked with the DataContractAttribute attribute. This includes support for the KnownTypeAttribute attribute on unmarked types.
To "opt out" of the serialization process for public members, properties, or fields, apply the IgnoreDataMemberAttribute attribute to that member.
Unmarked types (types without the DataContractAttribute attribute) can inherit from types that do have this attribute; however, the reverse is not permitted: types with the attribute cannot inherit from unmarked types. This rule is enforced primarily to ensure backward compatibility with code written in earlier versions of .NET Framework.