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] 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;
- }
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
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;
- }

Ignace KlougboPosted Jan 9, 2018, 2:57 AM
Using IsReference and IsRequired on and within the same datacontract won't work
Pradeep YadavPosted Jul 4, 2017, 5:19 AM
Nice One!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!