System.Runtime.Serialization
4.0.0.0
System.Runtime.Serialization.XmlObjectSerializer
Use the class to serialize and deserialize instances of a type into an XML stream or document. For example, you can create a type named Person with properties that contain essential data, such as a name and address. You can then create and manipulate an instance of the Person class and write all of its property values in an XML document for later retrieval, or in an XML stream for immediate transport. Most important, the is used to serialize and deserialize data sent in indigo1 messages. Apply the attribute to classes, and the attribute to class members to specify properties and fields that are serialized.
crlist types that can be serialized, see Types Supported by the Data Contract Serializer.
To use the , first create an instance of a class and an object appropriate to writing or reading the format; for example, an instance of the . Then call the method to persist the data. To retrieve data, create an object appropriate to reading the data format (such as an for an XML document) and call the method.
crabout using the , see Using Stand-alone Serialization.
You can set the type of a data contract serializer using the <dataContractSerializer> element in a client application configuration file.
Preparing Classes for Serialization or Deserialization
The is used in combination with the and classes. To prepare a class for serialization, apply the to the class. For each member of the class that returns data that you want to serialize, apply the . You can serialize fields and properties, regardless of accessibility: private, protected, internal, protected internal, or public.
For example, your schema specifies a Customer with an ID property, but you already have an existing application that uses a type named Person with a Name property. To create a type that conforms to the contract, first apply the to the class. Then apply the to every field or property that you want to serialize.
You can apply the to both private and public members.
The final format of the XML need not be text. Instead, the writes the data as an XML infoset, which allows you to write the data to any format recognized by the and . It is recommended that you use the and classes to read and write, because both are optimized to work with the .
If you are creating a class that has fields or properties that must be populated before the serialization or deserialization occurs, use callback attributes, as described in Version Tolerant Serialization Callbacks.
Adding to the Collection of Known Types
When serializing or deserializing an object, it is required that the type is "known" to the . Begin by creating an instance of a class that implements (such as ) and adding the known types to the collection. Then create an instance of the using one of the overloads that takes the (for example, .
Unlike other primitive types, the structure is not a known type by default, so it must be manually added to the list of known types (see Data Contract Known Types).
Forward Compatibility
The understands data contracts that have been designed to be compatible with future versions of the contract. Such types implement the interface. The interface features the property that returns an object. crdefault Forward Compatible Data Contracts.
Running under Partial Trust
When instantiating the target object during deserialization, the does not call the constructor of the target object. If you author a [DataContract] type that is accessible from partial trust (that is, it is public and in an assembly that has the AllowPartiallyTrustedCallers attribute applied) and that performs some security-related actions, you must be aware that the constructor is not called. In particular, the following techniques do not work:
-
If you try to restrict partial trust access by making the constructor internal or private, or by adding a LinkDemand to the constructor -- neither of these have any effect during deserialization under partial trust.
-
If you code the class that assumes the constructor has run, the class may get into an invalid internal state that is exploitable.
Serializes and deserializes an instance of a type into an XML stream or document using a supplied data contract. This class cannot be inherited.
Constructor
4.0.0.0
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type.
The type of the instances that are serialized or deserialized.
Constructor
4.0.0.0
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type, and a collection of known types that may be present in the object graph.
The type of the instances that are serialized or deserialized.
An of that contains the types that may be present in the object graph.
Constructor
4.0.0.0
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type and settings.
The type of the instance to serialize or deserialize.
The serializer settings.
Constructor
4.0.0.0
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type using the supplied XML root element and namespace.
The type of the instances that are serialized or deserialized.
The name of the XML element that encloses the content to serialize or deserialize.
The namespace of the XML element that encloses the content to serialize or deserialize.
Constructor
4.0.0.0
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type using the XML root element and namespace specified through the parameters of type .
The type of the instances that are serialized or deserialized.
An that contains the root element name of the content.
An that contains the namespace of the root element.
Constructor
4.0.0.0
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type. This method also specifies the root XML element and namespace in two string parameters as well as a list of known types that may be present in the object graph.
The type of the instances that are serialized or deserialized.
The root element name of the content.
The namespace of the root element.
An of that contains the types that may be present in the object graph.
Constructor
4.0.0.0
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type. This method also specifies the root XML element and namespace in two parameters as well as a list of known types that may be present in the object graph.
The type of the instances that are serialized or deserialized.
An that contains the root element name of the content.
An that contains the namespace of the root element.
A of that contains the known types that may be present in the object graph.
Constructor
4.0.0.0
To be added.
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, and a surrogate for custom serialization.
The type of the instances that are serialized or deserialized.
An of that contains the known types that may be present in the object graph.
true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
true to use non-standard XML constructs to preserve object reference data; otherwise, false.
An implementation of the to customize the serialization process.
Constructor
4.0.0.0
To be added.
This method is not available in Windows Store apps.
Initializes a new instance of the class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, and an alternative for mapping xsi:type declarations at run time.
The type of the instances that are serialized or deserialized.
An of that contains the known types that may be present in the object graph.
true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
true to use non-standard XML constructs to preserve object reference data; otherwise, false.
An implementation of the to customize the serialization process.
An implementation of the to map xsi:type declarations to data contract types.
Constructor
4.0.0.0
To be added.
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, and the XML element and namespace that contain the content.
The type of the instances that are serialized or deserialized.
The XML element that encloses the content to serialize or deserialize.
The namespace of the XML element that encloses the content to serialize or deserialize.
An of that contains the known types that may be present in the object graph.
true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
true to use non-standard XML constructs to preserve object reference data; otherwise, false.
An implementation of the to customize the serialization process.
Constructor
4.0.0.0
To be added.
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, and parameters of that specify the XML element and namespace that contain the content.
The type of the instances that are serialized or deserialized.
The that specifies the XML element that encloses the content to serialize or deserialize.
The that specifies the XML namespace of the root.
A of that contains the known types that may be present in the object graph.
true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
true to use non-standard XML constructs to preserve object reference data; otherwise, false.
An implementation of the to customize the serialization process.
Constructor
4.0.0.0
To be added.
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, the XML element and namespace that contains the content, and an alternative for mapping xsi:type declarations at run time.
The type of the instances that are serialized or deserialized.
The XML element that encloses the content to serialize or deserialize.
The namespace of the XML element that encloses the content to serialize or deserialize.
An of that contains the known types that may be present in the object graph.
true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
true to use non-standard XML constructs to preserve object reference data; otherwise, false.
An implementation of the to customize the serialization process.
An implementation of the to map xsi:type declarations to data contract types.
Constructor
4.0.0.0
To be added.
To be added.
Initializes a new instance of the class to serialize or deserialize an object of the specified type. This method also specifies a list of known types that may be present in the object graph, the maximum number of graph items to serialize, parameters to ignore unexpected data, whether to use non-standard XML constructs to preserve object reference data in the graph, a surrogate for custom serialization, parameters of that specify the XML element and namespace that contains the content, and an alternative for mapping xsi:type declarations at run time.
The type of the instances that are serialized or deserialized.
The XML element that encloses the content to serialize or deserialize.
The namespace of the XML element that encloses the content to serialize or deserialize.
An of that contains the known types that may be present in the object graph.
true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
true to use non-standard XML constructs to preserve object reference data; otherwise, false.
An implementation of the to customize the serialization process.
An implementation of the to map xsi:type declarations to data contract types.
Property
4.0.0.0
System.Runtime.Serialization.DataContractResolver
To be added.
The is set using a constructor.
Gets the component used to dynamically map xsi:type declarations to known contract types.
Property
4.0.0.0
System.Runtime.Serialization.IDataContractSurrogate
To be added.
The can be used to customize how instances of a type are serialized and deserialized. For example, it can be used to enable the serialization of legacy types that are not serializable, that is, types to which the attribute has not been applied.
Gets a surrogate type that can extend the serialization or deserialization process.
Property
4.0.0.0
System.Boolean
To be added.
The property is used when the class to which a has been applied also implements the interface. In this case, the data added in a future version of the contract is ignored on read and write. crdefault Forward Compatible Data Contracts.
Gets a value that specifies whether to ignore data supplied by an extension of the class when the class is being serialized or deserialized.
Method
4.0.0.0
System.Boolean
The determines whether it can read an object by checking that it is positioned on an XML element. It also examines the name and namespace of the XML element that the reader is positioned at and compares the values to the expected name and namespace. The expected name and namespace can be set with the following: the data contract name and namespace of the type passed into the constructor, or the and values passed into the constructor (if present).
Determines whether the is positioned on an object that can be deserialized.
true if the reader is at the start element of the stream to read; otherwise, false.
An used to read the XML stream.
Method
4.0.0.0
System.Boolean
To be added.
Determines whether the is positioned on an object that can be deserialized.
true if the reader is at the start element of the stream to read; otherwise, false.
The used to read the XML stream.
Property
4.0.0.0
System.Collections.ObjectModel.ReadOnlyCollection<System.Type>
To be added.
The property provides the set of known types that are used for serialization and deserialization. For example, if an instance of the class contains instances of a Person class, add the Person type to an instance of the class and use the instance to construct an instance of the . If you know of other types to add to the , then add those types to the collection.
Gets a collection of types that may be present in the object graph serialized using this instance of the .
Property
4.0.0.0
System.Int32
To be added.
This property can be set in configuration or imperatively in code. To set it in configuration, add a custom behavior to the behaviors section and add the following setting.
<behaviors> <behavior name="MyServiceBehavior"> <dataContractSerializer maxItemsInObjectGraph="3" /> </behavior></behaviors>
To set this property imperatively in code find the operation description of the service, then get the operation behavior, finally set the property as shown in the following code.
OperationDescription operation = host.Description.Endpoints[0].Contract.Operations.Find("MyOperationName");operation.Behaviors.Find<DataContractSerializerOperationBehavior>().MaxItemsInObjectGraph = 3;
The property specifies the maximum number of objects that the serializer serializes or deserializes in a single method call. (The method always reads one root object, but this object may have other objects in its data members. Those objects may have other objects, and so on.) The default is . Note that when serializing or deserializing arrays, every array entry counts as a separate object. Also, note that some objects may have a large memory representation and so this quota alone may not be sufficient to prevent Denial of Service attacks. crdefault Security Considerations for Data. If you need to increase this quota beyond its default value, it is important to do so both on the sending (serializing) and receiving (deserializing) sides. It applies both when reading and writing data.
Gets the maximum number of items in an object graph to serialize or deserialize.
Property
4.0.0.0
System.Boolean
To be added.
To be added.
Gets a value that specifies whether to use non-standard XML constructs to preserve object reference data.
Method
4.0.0.0
System.Object
To be added.
Reads the XML stream with an and returns the deserialized object.
The deserialized object.
The used to read the XML stream.
Method
4.0.0.0
System.Object
To be added.
Reads the XML stream with an and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value.
The deserialized object.
The used to read the XML stream.
true to check whether the name of the object corresponds to the root name value supplied in the constructor; otherwise, false.
Method
4.0.0.0
System.Object
To be added.
Reads the XML stream with an and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value.
The deserialized object.
The used to read the XML stream.
true to check whether the name of the object corresponds to the root name value supplied in the constructor; otherwise, false.
Method
4.0.0.0
System.Object
To be added.
To be added.
Reads an XML document or document stream and returns the deserialized object. The method includes a parameter to specify whether the object name is verified is validated, and a resolver for mapping xsi:type declarations at runtime.
The deserialized object.
The XML reader used to read the content.
true to verify the object name; otherwise, false.
Property
4.0.0.0
System.Boolean
To be added.
To be added.
Gets a value that specifies whether read-only types are serialized.
Method
4.0.0.0
System.Void
To be added.
Writes the closing XML element using an .
The used to write the stream.
Method
4.0.0.0
System.Void
To be added.
Writes the closing XML element using an .
The used to write the stream.
Method
4.0.0.0
System.Void
To be added.
Writes all the object data (starting XML element, content, and closing element) to an XML document or stream with an .
The used to write the XML document or stream.
The object that contains the data to write to the stream.
Method
4.0.0.0
System.Void
To be added.
To be added.
Writes all the object data (starting XML element, content, and enclosing element) to an XML document or stream using the specified XmlDictionaryWriter. The method includes a resolver for mapping xsi:type declarations at runtime.
An XmlDictionaryWriter used to write the content to the XML document or stream.
The object that contains the content to write.
Method
4.0.0.0
System.Void
To be added.
Writes the XML content using an .
The used to write the stream.
The object to write to the stream.
Method
4.0.0.0
System.Void
To be added.
Writes the XML content using an .
The used to write the stream.
The object to write to the stream.
Method
4.0.0.0
System.Void
To be added.
Writes the opening XML element using an .
The used to write the XML start element.
The object to write.
Method
4.0.0.0
System.Void
To be added.
Writes the opening XML element using an .
The used to write the XML start element.
The object to write.