System.Runtime.Serialization.DataContracts
Namespace: System.Runtime.Serialization
This namespace provides types that enable you to define and manage data contracts, which are fundamental to Windows Communication Foundation (WCF) and other serialization scenarios. Data contracts define the shape of the data that is exchanged between services or serialized to various formats.
Key Types
DataContractAttribute: Specifies that a class or structure is a data contract.DataMemberAttribute: Specifies that a public serializable field, property, or parameter is a data member.IDataContractSurrogate: Enables you to customize the data contract model.DataContractSerializer: Serializes and deserializes objects using the data contract model.
DataContractAttribute Class
Namespace: System.Runtime.Serialization
Specifies that a class or structure is a data contract. This attribute is applied to types to indicate they are designed for serialization using the Data Contract serialization mechanism.
Properties
IsNameSetExplicitly: Gets a value indicating whether the name is explicitly set.Name: Gets or sets the name of the data contract.
Remarks
When a type is marked with the DataContractAttribute, only members marked with the DataMemberAttribute are considered for serialization. This provides fine-grained control over what data is exposed.
DataMemberAttribute Class
Namespace: System.Runtime.Serialization
Specifies that a public serializable field, property, or parameter is a data member. This attribute is applied to individual members within a type marked with DataContractAttribute.
Properties
EmitDefaultValue: Gets or sets a value that indicates whether the data member's value is serialized when it is set to its default value.IsNameSetExplicitly: Gets a value indicating whether the name is explicitly set.Name: Gets or sets the name of the data member.Order: Gets or sets the order in which members are serialized.
Remarks
By default, all public members of a data contract type are serialized. However, using DataMemberAttribute allows you to explicitly include or exclude members and control their serialization behavior.
IDataContractSurrogate Interface
Namespace: System.Runtime.Serialization
Enables you to customize the data contract model. This interface allows for advanced scenarios such as mapping types, specifying surrogate types, and controlling the deserialization process.
Methods
GetSurrogateType(Type type): Returns a surrogate type that can be used to provide a different representation of the original type.GetKnownTypes(Type declaredType): Returns a collection of known types for a given declared type.GetField(Type dataContractType, string name): Returns a FieldInfo object for the specified data contract type and member name.GetProperty(Type dataContractType, string name): Returns a PropertyInfo object for the specified data contract type and member name.
DataContractSerializer Class
Namespace: System.Runtime.Serialization
Serializes and deserializes objects using the data contract model. This class is the primary mechanism for performing data contract serialization and deserialization.
Constructors
DataContractSerializer()DataContractSerializer(Type rootType)DataContractSerializer(Type rootType, string rootName, string rootNamespace)
Methods
ReadObject(Stream stream): Deserializes an object from the specified stream.WriteObject(Stream stream, object graph): Serializes an object to the specified stream.IsStartObject(XmlDictionaryReader reader): Checks if the current node in the XML reader is the start of a data contract object.ReadObject(XmlDictionaryReader reader): Deserializes an object from the specified XML reader.
Example Usage
// Define a data contract
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
}
// Serialize an object
Person person = new Person { Name = "Alice", Age = 30 };
DataContractSerializer serializer = new DataContractSerializer(typeof(Person));
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, person);
string xmlString = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(xmlString);
}
// Deserialize an object
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
{
Person deserializedPerson = (Person)serializer.ReadObject(ms);
Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");
}