Data Member attribute
Data Member are the fields or properties of your Data Contract class. You must specify [DataMember] attribute on the property or the field of your Data Contract class to identify it as a Data Member. DataContractSerializer will serialize only those members, which are annotated by [DataMemeber] attribute. You can define Data Members by just placing [DataMemeber] attribute on the property or the field.
Example
- [DataContract(IsReference =true,Name ="StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]
- public class Student
- {
- [DataMember]
- public int ID=0;
- [DataMember]
- public string Name=null;
- [DataMember]
- public int Fees = 500;
- }
[DataMember] attribute parameters
[DataMember] has some parameters, which specify the details about Data Member. The name of these parameters and their functions are given below.
EmitDefaultValue
This parameter specifies that whether or not, you are required to serialize the default value of a data member. Its default value is true.
Example
- [DataContract(IsReference =true,Name ="StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]
- public class Student
- {
- [DataMember]
- public int ID=0;
- [DataMember]
- public string Name=null;
- [DataMember(EmitDefaultValue =false)]
- public int Fees = 500;
- }
In the example given above, the default value of ID and Name will be serialized as their EmitDefaultValue is true , while the value of Fees will not be serialized as its EmitDefaultValue is false
IsRequired
This parameter is used to specify whether the data member is required or not. The true value of this parameter informs serialization engine that this data member must be present.
Example
- [DataContract(IsReference = true, Name = "StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]
- public class Student {
- [DataMember(IsRequired = true)]
- public int ID = 0;
- [DataMember]
- public string Name = null;
- [DataMember(EmitDefaultValue = false, IsRequired = false)]
- public int Fees = 500;
- }
In the example given above, IsRequired true parametervalue of ID Data Member tells the serialization engine that it is required and Fees is not required.
Name
This parameter is used to get or set the name of the data member. The default value of this parameter is the name of the property or the field on which it is applied.
Example
- [DataContract(IsReference = true, Name = "StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]
- public class Student {
- [DataMember(IsRequired = true)]
- public int ID = 0;
- [DataMember(Name = "StudentName")]
- public string Name = null;
- [DataMember(EmitDefaultValue = false, IsRequired = false)]
- public int Fees = 500;
- }
Order
The order parameter determines the order in which the data is sent or received. It also specifies the serialization and deserialization order of a data member
Example
- [DataContract(IsReference =true,Name ="StudentInformation", Namespace = "http://www.StudentInformationManagement.com")]
- public class Student
- {
- [DataMember(IsRequired =true,Order =0)]
- public int ID=0;
- [DataMember(Name ="StudentName",Order =1)]
- public string Name=null;
- [DataMember(EmitDefaultValue =false,IsRequired =false,Order =2)]
- public int Fees = 500;
- }
Data Member attribute is very useful for defining the rules or policies for Data Members of your Data Contract.