For transferring messages, and from a security prospective, we need to make a data/message in a serialized format. For serializing data we use [datacontract] and [datamember] attributes.
In this chapter, we will see how to add attributes to a DataContract and Data Member. In our previous chapter, we saw that a DataContract is nothing but a simple C# class and the Data Member is the member of the class, it might be any data type.
In C# WCF, a DataContract is a .NET attribute that defines the structure of the data to be exchanged. By marking a class with the [DataContract] attribute, you specify that instances of that class can be serialized and deserialized for communication across the network.
Specifies that the type defines or implements a datacontract and is serializable by a serializer, such as the DataContractSerializer. To make their type serializable, type authors must define a datacontract for their type.
DataContractAttribute Class is in the System.Runtime.Serialization namespace. You should add a reference to System.Runtime.Serialization.dll. That assembly isn't referenced by default though. To add the reference to your project you have to go to References -> Add Reference in the Solution Explorer and add an assembly reference manually. CD..
Serializes and deserializes an instance of a type into an XML stream or document using a supplied datacontract. This class cannot be inherited. For more information about this API, see Supplemental API remarks for DataContractSerializer.
For Deserialize, it might be more efficient to replace the GetBytes/Write with "using (StreamWriter writer = new StreamWriter (stream, encoding) { writer.Write (xml); ... }" to avoid the extra byte [] buffer. This is awesome, and still incredibly helpful. Most importantly, this is correct.
This topic shows the basic steps to create a datacontract using a class or structure. For more information about datacontracts and how they are used, see Using DataContracts.
What are you trying to do with those two datacontracts? Yes, that would work. The DataContractAttribute has Inherited set to false, so it is necessary to apply the attribute to both the child class and the parent class (as you have done in the question).