[DataMember] Attribute in WCF

The [DataMember] attribute is applied to all members of the data contract type to identify it as a data member. Specifying this attribute identifies that member as member whose data will be serialized by the DataContractSerializer.
Just like the [DataContract] attribute, the [DataMember] attribute has a number of parameters to help specify details of the data member.

EmitDefaultValue

The EmitDefaultValue property specifies whether or not to serialize the default value for a field or property being serialized. Types have a concept of default values within the .NET Framework. As such, WCF can take advantage of this by omitting a data member from the serialized data because it has a default value and the data member is set to the default value.
The default value for this property is true, so to take advantage of this functionality the property value must be set to false. In fact, setting the property value to true is not recommended and should only be used to decrease the size of the message or for interoperability reasons.
The following sets the EmitDefaultValue property to false on several data members:

[DataContract]
public class BookOrder
{
    [DataMember]
    public string BookTitle = null;
    [DataMember]
    public int OrderQuantity = 0;
    [DataMember(EmitDefaultValue=false)]
    public string Address2 = null;
    [DataMember(EmitDefaultValue=false)]
    public string Country = null;
}

In this example, the BookTitle and OrderQuantity values will be written because the EmitDefaultValue property is defaulting to true, yet the Address2 and Country data members will not be written because the EmitDefaultValue for those data members have specifically been set to false.

IsRequired

The IsRequired parameter is used to inform the serialization engine that a data member must be present. A value of true indicates that a member must be present. If false, a member is not required. The following example tells the serialization engine that the symbol member of the data contract is not required:

[DataContract]
public class BuyStock
{
    [DataMember(IsRequired=”false”]
    public string symbol;
}

Name

The Name property is used to get or set the name of the data member. This property overrides the default name of the data member. If this property is not specified, the default name is taken from the method that the attribute is applied to.
In the following example, the Name property sets the name to UserLogin:

[DataContract]
public class BuyStock
{
    [DataMember(Name=”Symb”]
    public string symbol;
}

Had the Name property not been specified, the default would have been Symbol.

Order

The Order parameter is used to specify the order in which the data members are serialized and deserialized. In other words, the order determines how the data is to be sent or received (and as such, where it will be physically located) in the XML.
The following example illustrates how to specify the processing order of the data members:

[DataContract]
public class BuyStock
{
    [DataMember(Order=0)]
    public string symbol;
    [DataMember(Order=1)]
    public int quantity;
    [DataMember(Order=2)]
    public decimal price;
}

As a note, there are a few rules when determining order:

  • If a data contract type is part of an inheritance hierarchy, data members of its base types are always ordered first.
  • The current type’s data members that do not have the Order property set are next in order, in ascending order.
  • Any members that have the Order property set follow next. The order of these are determined by the Order property first, then alphabetically if more than one of a certain order exists.
Tagged . Bookmark the permalink.

Leave a Reply