Class DataContractSerializer<TDataContract>
Serializes and deserializes an instance of a type into an XML stream or document using a supplied data contract. This class cannot be inherited.
Inheritance
Namespace: Copper.Common
Assembly: Copper.Common.dll
Syntax
public sealed class DataContractSerializer<TDataContract>
where TDataContract : class
Type Parameters
| Name | Description |
|---|---|
| TDataContract | The type of the data contract. |
Constructors
View SourceDataContractSerializer()
Initializes a new instance of the System.Runtime.Serialization.DataContractSerializer class to serialize or deserialize an object of the specified type.
Declaration
public DataContractSerializer()
DataContractSerializer(IEnumerable<Type>)
Initializes a new instance of the System.Runtime.Serialization.DataContractSerializer class to serialize or deserialize an object of the specified type, and a collection of known types that may be present in the TDataContract graph.
Declaration
public DataContractSerializer(IEnumerable<Type> knownTypes)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<System.Type> | knownTypes | An System.Collections.Generic.IEnumerable<T> of System.Type that contains the types that may be present in the TDataContract graph. |
DataContractSerializer(DataContractSerializerSettings)
Initializes a new instance of the System.Runtime.Serialization.DataContractSerializer class to serialize or deserialize an object of the specified type and settings.
Declaration
public DataContractSerializer(DataContractSerializerSettings settings)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Runtime.Serialization.DataContractSerializerSettings | settings | The serializer settings. |
DataContractSerializer(String, String)
Initializes a new instance of the System.Runtime.Serialization.DataContractSerializer class to serialize or deserialize an object of the specified type using the supplied XML root element and namespace.
Declaration
public DataContractSerializer(string rootName, string rootNamespace)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | rootName | The name of the XML element that encloses the content to serialize or deserialize. |
| System.String | rootNamespace | The namespace of the XML element that encloses the content to serialize or deserialize. |
DataContractSerializer(String, String, IEnumerable<Type>)
Initializes a new instance of the System.Runtime.Serialization.DataContractSerializer 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 TDataContract graph.
Declaration
public DataContractSerializer(string rootName, string rootNamespace, IEnumerable<Type> knownTypes)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | rootName | The root element name of the content. |
| System.String | rootNamespace | The namespace of the root element. |
| System.Collections.Generic.IEnumerable<System.Type> | knownTypes | An System.Collections.Generic.IEnumerable<T> of System.Type that contains the types that may be present in the TDataContract graph. |
DataContractSerializer(XmlDictionaryString, XmlDictionaryString)
Initializes a new instance of the System.Runtime.Serialization.DataContractSerializer class to serialize or deserialize an object of the specified type using the XML root element and namespace specified through the parameters of type System.Xml.XmlDictionaryString.
Declaration
public DataContractSerializer(XmlDictionaryString rootName, XmlDictionaryString rootNamespace)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlDictionaryString | rootName | An System.Xml.XmlDictionaryString that contains the root element name of the content. |
| System.Xml.XmlDictionaryString | rootNamespace | An System.Xml.XmlDictionaryString that contains the namespace of the root element. |
DataContractSerializer(XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)
Initializes a new instance of the System.Runtime.Serialization.DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies the root XML element and namespace in two System.Xml.XmlDictionaryString parameters as well as a list of known types that may be present in the TDataContract graph.
Declaration
public DataContractSerializer(XmlDictionaryString rootName, XmlDictionaryString rootNamespace, IEnumerable<Type> knownTypes)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlDictionaryString | rootName | An System.Xml.XmlDictionaryString that contains the root element name of the content. |
| System.Xml.XmlDictionaryString | rootNamespace | An System.Xml.XmlDictionaryString that contains the namespace of the root element. |
| System.Collections.Generic.IEnumerable<System.Type> | knownTypes | A System.Collections.Generic.IEnumerable<T> of System.Type that contains the known types that may be present in the TDataContract graph. |
Properties
View SourceDataContractResolver
Gets the component used to dynamically map langword_csharp_xsi:type declarations to known contract types.
Declaration
public DataContractResolver DataContractResolver { get; }
Property Value
| Type | Description |
|---|---|
| System.Runtime.Serialization.DataContractResolver | An implementation of the System.Runtime.Serialization.DataContractResolver class. |
IgnoreExtensionDataObject
Gets a value that specifies whether to ignore data supplied by an extension of the class when the class is being serialized or deserialized.
Declaration
public bool IgnoreExtensionDataObject { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean | true to omit the extension data; otherwise, false. |
KnownTypes
Gets a collection of types that may be present in the TDataContract graph serialized using this instance of the System.Runtime.Serialization.DataContractSerializer.
Declaration
public ReadOnlyCollection<Type> KnownTypes { get; }
Property Value
| Type | Description |
|---|---|
| System.Collections.ObjectModel.ReadOnlyCollection<System.Type> | A System.Collections.ObjectModel.ReadOnlyCollection<T> that contains the expected types passed in as known types to the System.Runtime.Serialization.DataContractSerializer constructor. |
MaxItemsInObjectGraph
Gets the maximum number of items in an TDataContract graph to serialize or deserialize.
Declaration
public int MaxItemsInObjectGraph { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | The maximum number of items to serialize or deserialize. The default is System.Int32.MaxValue. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException | The number of items exceeds the maximum value. |
PreserveObjectReferences
Gets a value that specifies whether to use non-standard XML constructs to preserve object reference data.
Declaration
public bool PreserveObjectReferences { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean | true to keep the references; otherwise, false. |
SerializeReadOnlyTypes
Gets a value that specifies whether read-only types are serialized.
Declaration
public bool SerializeReadOnlyTypes { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean | true if read-only types are serialized; false if all types are serialized. |
Methods
View SourceIsStartObject(XmlDictionaryReader)
Determines whether the System.Xml.XmlDictionaryReader is positioned on an object that can be deserialized.
Declaration
public bool IsStartObject(XmlDictionaryReader reader)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlDictionaryReader | reader | An System.Xml.XmlDictionaryReader used to read the XML stream. |
Returns
| Type | Description |
|---|---|
| System.Boolean | true if the reader is at the start element of the stream to read; otherwise, false. |
IsStartObject(XmlReader)
Determines whether the System.Xml.XmlReader is positioned on an object that can be deserialized.
Declaration
public bool IsStartObject(XmlReader reader)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlReader | reader | The System.Xml.XmlReader used to read the XML stream. |
Returns
| Type | Description |
|---|---|
| System.Boolean | true if the reader is at the start element of the stream to read; otherwise, false. |
ReadObject(Stream)
Reads the XML stream or document with a System.IO.Stream and returns the deserialized object.
Declaration
public TDataContract ReadObject(Stream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.Stream | stream | A System.IO.Stream used to read the XML stream or document. |
Returns
| Type | Description |
|---|---|
| TDataContract | The deserialized object. |
ReadObject(String, Encoding)
Reads the XML string with a System.IO.Stream and returns the deserialized object.
Declaration
public TDataContract ReadObject(string value, Encoding encoding = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | value | A XMLSystem.String string. |
| System.Text.Encoding | encoding | The System.Text.Encoding when reading the value string. |
Returns
| Type | Description |
|---|---|
| TDataContract | The deserialized object. |
ReadObject(XmlDictionaryReader, Boolean)
Reads the XML stream with an System.Xml.XmlDictionaryReader and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value.
Declaration
public TDataContract ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlDictionaryReader | reader | The System.Xml.XmlDictionaryReader used to read the XML stream. |
| System.Boolean | verifyObjectName | true to check whether the name of the object corresponds to the root name value supplied in the constructor; otherwise, false. |
Returns
| Type | Description |
|---|---|
| TDataContract | The deserialized object. |
Exceptions
| Type | Condition |
|---|---|
| System.Runtime.Serialization.SerializationException | The |
ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)
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 langword_csharp_xsi:type declarations at runtime.
Declaration
public TDataContract ReadObject(XmlDictionaryReader reader, bool verifyObjectName, DataContractResolver dataContractResolver)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlDictionaryReader | reader | The XML reader used to read the content. |
| System.Boolean | verifyObjectName | true to verify the object name; otherwise, false. |
| System.Runtime.Serialization.DataContractResolver | dataContractResolver | An implementation of the System.Runtime.Serialization.DataContractResolver to map langword_csharp_xsi:type declarations to data contract types. |
Returns
| Type | Description |
|---|---|
| TDataContract | The deserialized object. |
ReadObject(XmlReader)
Reads the XML stream with an System.Xml.XmlReader and returns the deserialized object.
Declaration
public TDataContract ReadObject(XmlReader reader)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlReader | reader | The System.Xml.XmlReader used to read the XML stream. |
Returns
| Type | Description |
|---|---|
| TDataContract | The deserialized object. |
ReadObject(XmlReader, Boolean)
Reads the XML stream with an System.Xml.XmlReader and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value.
Declaration
public TDataContract ReadObject(XmlReader reader, bool verifyObjectName)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlReader | reader | The System.Xml.XmlReader used to read the XML stream. |
| System.Boolean | verifyObjectName | true to check whether the name of the object corresponds to the root name value supplied in the constructor; otherwise, false. |
Returns
| Type | Description |
|---|---|
| TDataContract | The deserialized object. |
Exceptions
| Type | Condition |
|---|---|
| System.Runtime.Serialization.SerializationException | The |
WriteEndObject(XmlDictionaryWriter)
Writes the closing XML element using an System.Xml.XmlDictionaryWriter.
Declaration
public void WriteEndObject(XmlDictionaryWriter writer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlDictionaryWriter | writer | The System.Xml.XmlDictionaryWriter used to write the stream. |
Exceptions
| Type | Condition |
|---|---|
| System.Runtime.Serialization.InvalidDataContractException | The type being serialized does not conform to data contract rules. For example, the System.Runtime.Serialization.DataContractAttribute attribute has not been applied to the type. |
| System.Runtime.Serialization.SerializationException | There is a problem with the instance being written. |
WriteEndObject(XmlWriter)
Writes the closing XML element using an System.Xml.XmlWriter.
Declaration
public void WriteEndObject(XmlWriter writer)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlWriter | writer | The System.Xml.XmlWriter used to write the stream. |
Exceptions
| Type | Condition |
|---|---|
| System.Runtime.Serialization.InvalidDataContractException | The type being serialized does not conform to data contract rules. For example, the System.Runtime.Serialization.DataContractAttribute attribute has not been applied to the type. |
| System.Runtime.Serialization.SerializationException | There is a problem with the instance being written. |
WriteObject(TDataContract)
Returns the complete content (start, content, and end) of the object as a XML System.String.
Declaration
public string WriteObject(TDataContract graph)
Parameters
| Type | Name | Description |
|---|---|---|
| TDataContract | graph | The object that contains the data to return as a string. |
Returns
| Type | Description |
|---|---|
| System.String | The XML string representation of the object. |
Exceptions
| Type | Condition |
|---|---|
| System.Runtime.Serialization.InvalidDataContractException | the type being serialized does not conform to data contract rules. For example, the System.Runtime.Serialization.DataContractAttribute attribute has not been applied to the type. |
| System.Runtime.Serialization.SerializationException | there is a problem with the instance being serialized. |
| System.ServiceModel.QuotaExceededException | the maximum number of objects to serialize has been exceeded. Check the System.Runtime.Serialization.DataContractSerializer.MaxItemsInObjectGraph property. |
WriteObject(Stream, TDataContract)
Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified System.IO.Stream.
Declaration
public void WriteObject(Stream stream, TDataContract graph)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.Stream | stream | A System.IO.Stream used to write the XML document or stream. |
| TDataContract | graph | The object that contains the data to write to the stream. |
Exceptions
| Type | Condition |
|---|---|
| System.Runtime.Serialization.InvalidDataContractException | the type being serialized does not conform to data contract rules. For example, the System.Runtime.Serialization.DataContractAttribute attribute has not been applied to the type. |
| System.Runtime.Serialization.SerializationException | there is a problem with the instance being serialized. |
| System.ServiceModel.QuotaExceededException | the maximum number of objects to serialize has been exceeded. Check the System.Runtime.Serialization.DataContractSerializer.MaxItemsInObjectGraph property. |
WriteObject(XmlDictionaryWriter, TDataContract, DataContractResolver)
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 langword_csharp_xsi:type declarations at runtime.
Declaration
public void WriteObject(XmlDictionaryWriter writer, TDataContract graph, DataContractResolver dataContractResolver)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlDictionaryWriter | writer | An XmlDictionaryWriter used to write the content to the XML document or stream. |
| TDataContract | graph | The object that contains the content to write. |
| System.Runtime.Serialization.DataContractResolver | dataContractResolver | An implementation of the System.Runtime.Serialization.DataContractResolver used to map langword_csharp_xsi:type declarations to known data contracts. |
WriteObject(XmlWriter, TDataContract)
Writes all the object data (starting XML element, content, and closing element) to an XML document or stream with an System.Xml.XmlWriter.
Declaration
public void WriteObject(XmlWriter writer, TDataContract graph)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlWriter | writer | The System.Xml.XmlWriter used to write the XML document or stream. |
| TDataContract | graph | The object that contains the data to write to the stream. |
Exceptions
| Type | Condition |
|---|---|
| System.Runtime.Serialization.InvalidDataContractException | The type being serialized does not conform to data contract rules. For example, the System.Runtime.Serialization.DataContractAttribute attribute has not been applied to the type. |
| System.Runtime.Serialization.SerializationException | There is a problem with the instance being written. |
WriteObjectContent(XmlDictionaryWriter, TDataContract)
Writes the XML content using an System.Xml.XmlDictionaryWriter.
Declaration
public void WriteObjectContent(XmlDictionaryWriter writer, TDataContract graph)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlDictionaryWriter | writer | The System.Xml.XmlDictionaryWriter used to write the stream. |
| TDataContract | graph | The object to write to the stream. |
Exceptions
| Type | Condition |
|---|---|
| System.Runtime.Serialization.InvalidDataContractException | The type being serialized does not conform to data contract rules. For example, the System.Runtime.Serialization.DataContractAttribute attribute has not been applied to the type. |
| System.Runtime.Serialization.SerializationException | There is a problem with the instance being written. |
WriteObjectContent(XmlWriter, TDataContract)
Writes the XML content using an System.Xml.XmlWriter.
Declaration
public void WriteObjectContent(XmlWriter writer, TDataContract graph)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlWriter | writer | The System.Xml.XmlWriter used to write the stream. |
| TDataContract | graph | The object to write to the stream. |
Exceptions
| Type | Condition |
|---|---|
| System.Runtime.Serialization.InvalidDataContractException | The type being serialized does not conform to data contract rules. For example, the System.Runtime.Serialization.DataContractAttribute attribute has not been applied to the type. |
| System.Runtime.Serialization.SerializationException | There is a problem with the instance being written. |
WriteStartObject(XmlDictionaryWriter, TDataContract)
Writes the opening XML element using an System.Xml.XmlDictionaryWriter.
Declaration
public void WriteStartObject(XmlDictionaryWriter writer, TDataContract graph)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlDictionaryWriter | writer | The System.Xml.XmlDictionaryWriter used to write the XML start element. |
| TDataContract | graph | The object to write. |
WriteStartObject(XmlWriter, TDataContract)
Writes the opening XML element using an System.Xml.XmlWriter.
Declaration
public void WriteStartObject(XmlWriter writer, TDataContract graph)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Xml.XmlWriter | writer | The System.Xml.XmlWriter used to write the XML start element. |
| TDataContract | graph | The object to write. |