Original address: Click to open the link
DataContractSerializer class
Serializes and deserializes a type instance into an XML stream or document using the provided data contract. This class cannot be inherited.
namespaces: System.Runtime.Serialization
Assemblies: System.Runtime.Serialization (in System.Runtime.Serialization.dll)
Note
Use the DataContractSerializer class to serialize and deserialize an instance of a type into an XML stream or document. You can specify the properties and fields to serialize by applying the DataContractAttribute attribute to the class and applying the DataMemberAttribute attribute to the class member.
From the literal meaning to understand is: Data contract serialization, this article is mainly to explain the use of DataContractSerializer serialization and deserialization.
DataContractAttribute and Datamenmberattribute
------------------------------------------------------------//Copyright (c) Microsoft Corporation. All rights reserved.//------------------------------------------------------------namespace system.runtime.serialization{//Summary://Specifies that the type is to define or implement a data contract and can be used by serializers such as System.Runtime.Serialization.DataContractSeria Lizer) is serialized. To make the type serializable, the type author must define a data contract for its type. [AttributeUsage (AttributeTargets.Class | attributetargets.struct | Attributetargets.enum, inherited = False, AllowMultiple = false)] public sealed class Datacontractattribute:attribute {string name; string ns; BOOL isnamesetexplicitly; BOOL isnamespacesetexplicitly; BOOL IsReference; BOOL isreferencesetexplicitly;//Summary://Initializes a new instance of the System.Runtime.Serialization.DataContractAttribute class. Public DataContractAttribute () {}//Summary://Gets or sets a value that indicates whether the object reference data is persisted. Return Result://True if object reference data is reserved using standard XML; otherwise fAlse. The default value is False. public bool IsReference {get {return isreference;} set {isreference = value; isreferencesetexplicitly = true; }} public bool isreferencesetexplicitly {get {return isreferencesetexplicitly;} }////Summary://Gets or sets the namespace of the type's data contract. Returns the namespace of the result://contract. public string Namespace {get {return ns;} set {ns = value; isnamespacesetexplicitly = true; }} public bool isnamespacesetexplicitly {get {return isnamespacesetexplicitly;} }////Summary://Gets or sets the name of the data contract for the type. Returns the result: The local name of the//data contract. The default value is the name of the class to which the property is applied. public string Name {get {return Name;} set {name = value; isnamesetexplicitly = true; }} public bool isnamesetexplicitly {get {return isnamesetexplicitly;} } }}
DataContractAttribute
From the AttributeUsageAttribute feature applied on DataContractAttribute, this attribute can only be used for enumerations, classes, structs, and not for interfaces. From the keyword sealed know that DataContractAttribute is not inherited. The Allowmutiple property is False, indicating that only one DataContractAttribute attribute can be applied on a data type.
The DataContractAttribute definition above shows that DataContractAttribute contains only 5 attribute members, where name and namespace represent the name and namespace of the data contract. IsReference indicates whether the object's existing reference structure is persisted at the time of serialization, and the default value of this property is false.
Data contract members adopt a display selection mechanism, that is, properties/fields of data types that have the DataContractAttribute attribute applied do not automatically generate contract data members, only those attributes that have the DataMemberAttribute attribute applied A field is a member of a data contract.
------------------------------------------------------------//Copyright (c) Microsoft Corporation. All rights reserved.//------------------------------------------------------------namespace system.runtime.serialization{//Abstract://When applied to a member of a type, specifies that the member is part of the data contract and can be used by the System.Runtime.Serialization.DataContractSeri Alizer//Serialization. [AttributeUsage (Attributetargets.field | Attributetargets.property, inherited = False, AllowMultiple = false)] public sealed class Datamemberattribute:attribu Te {string name; BOOL isnamesetexplicitly; int order =-1; BOOL IsRequired; bool Emitdefaultvalue = Globals.defaultemitdefaultvalue; Summary://Initializes a new instance of the System.Runtime.Serialization.DataMemberAttribute class. Public DataMemberAttribute () {}////Summary://Gets or sets the data member name. Returns the result://The name of the data member. The default value is the name of the target to which the property is applied. public string Name {get {return name}; } set {name = value; isnamesetexplicitly = true;} } public bool Isnamesetexplicitly {get {return isnamesetexplicitly;} }////Summary://Gets or sets the order in which members are serialized and deserialized. Returns the result://The numeric order of serialization or deserialization. public int Order {get {return Order;} set {if (value < 0) throw System.Runtime.Serialization.DiagnosticUtility. Exceptionutility.throwhelpererror New Invaliddatacontractexception (SR. GetString (SR. (ordercannotbenegative))); order = value; }//Summary://Gets or sets a value that indicates that the serialization engine must exist when it is read or deserialized. Returns the result://True if the member is required; otherwise false. Exception://System.Runtime.Serialization.SerializationException://The member does not exist. public bool IsRequired {get {return isrequired;} set {IsrequireD = value; }}//Summary://Gets or sets a value that specifies whether the default value of the field or property being serialized is serialized. Returns the result://TRUE if the default value of the member should be generated in the serialized stream; otherwise false. The default value is true. public bool Emitdefaultvalue {get {return emitdefaultvalue;} set {Emitdefaultvalue = value;} } }}
DataMemberAttribute
This attribute can only be applied to fields and attributes from the AttributeUsageAttribute feature applied above on DataMemberAttribute. Because only these two elements are members of the data. The 4 attributes represent the following meanings, respectively.
Name: Names of data members, default to the name of the field or property.
Order: the location where the corresponding data member appears in the final serialized XML, the smaller the order value, the default value is-1 before the test.
IsRequired: Indicates whether the attribute member is a required member. The default value is False, which indicates that the member can be defaulted.
Emitdefaultvalue: Gets or sets a value that specifies whether to serialize the default value of the field or property being serialized. True if the default value of the member should be generated in the serialized stream; otherwise false. The default value is true.
Source code Source: Click to open the link
The above is the C # WCF DataContractSerializer class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!