Serializing Collections: Enumeration Types and Data Contracts

Description Article
Table of Contents
Operations
Serialization of simple enumerations with DataContract attribute applied

If the DataContract attribute is applied to the enumeration type then the DataMember attribute must be used on each member that must be included in the data contract.

How to do it
[DataContract]
public class Car
{
    [DataMember]
    public string model;
    [DataMember]
    public CarConditionEnum condition;
}

[DataContract(Name = "CarCondition")]
public enum CarConditionEnum
{
    [EnumMember]
    New,
    [EnumMember]
    Used,
    [EnumMember]
    Rental,
    Broken,
    Stolen
}
Serialization of simple enumerations without DataContract Attribute applied

If the DataContract attribute has not been applied on the enumeration type
then it is considered as serializable.

The NonSerialized attribute is used to exclude enumeration members.

Remarks

(i) If the EnumMember attribute to a member it has no effect.
(ii) The Serializable attribute is ignored if applied to the enumeration.

How to do it
public enum CarCondition
{
    New,
    Used,
    Rental,
    [NonSerialized]
    Lost
}