แก้ไข

แชร์ผ่าน


DataContractSerializer Constructors

Definition

Initializes a new instance of the DataContractSerializer class.

Overloads

DataContractSerializer(Type)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type.

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

Initializes a new instance of the DataContractSerializer 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 XmlDictionaryString that specify the XML element and namespace that contains the content, and an alternative for mapping xsi:type declarations at run time.

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

Initializes a new instance of the DataContractSerializer 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.

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

Initializes a new instance of the DataContractSerializer 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 XmlDictionaryString that specify the XML element and namespace that contain the content.

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

Initializes a new instance of the DataContractSerializer 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.

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

Initializes a new instance of the DataContractSerializer 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.

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

Initializes a new instance of the DataContractSerializer 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.

DataContractSerializer(Type, String, String, IEnumerable<Type>)

Initializes a new instance of the 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 object graph.

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)

Initializes a new instance of the 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 XmlDictionaryString.

DataContractSerializer(Type, String, String)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type using the supplied XML root element and namespace.

DataContractSerializer(Type, DataContractSerializerSettings)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type and settings.

DataContractSerializer(Type, IEnumerable<Type>)

Initializes a new instance of the DataContractSerializer 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.

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies the root XML element and namespace in two XmlDictionaryString parameters as well as a list of known types that may be present in the object graph.

DataContractSerializer(Type)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type.

public:
 DataContractSerializer(Type ^ type);
public DataContractSerializer (Type type);
new System.Runtime.Serialization.DataContractSerializer : Type -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type)

Parameters

type
Type

The type of the instances that are serialized or deserialized.

Examples

The following example creates an instance of the DataContractSerializer that specifies the type to serialize or deserialize.

public static void Constructor1()
{
    // Create an instance of the DataContractSerializer.
    DataContractSerializer ser =
        new DataContractSerializer(typeof(Person));
    // Other code not shown.
}
 Public Shared Sub Constructor1() 
     ' Create an instance of the DataContractSerializer.
     Dim ser As New DataContractSerializer(GetType(Person))

     ' Other code not shown.    
 End Sub

Applies to

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

Initializes a new instance of the DataContractSerializer 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 XmlDictionaryString that specify the XML element and namespace that contains the content, and an alternative for mapping xsi:type declarations at run time.

public:
 DataContractSerializer(Type ^ type, System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate, System::Runtime::Serialization::DataContractResolver ^ dataContractResolver);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate, System.Runtime.Serialization.DataContractResolver dataContractResolver);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate * System.Runtime.Serialization.DataContractResolver -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate, dataContractResolver As DataContractResolver)

Parameters

type
Type

The type of the instances that are serialized or deserialized.

rootName
XmlDictionaryString

The XML element that encloses the content to serialize or deserialize.

rootNamespace
XmlDictionaryString

The namespace of the XML element that encloses the content to serialize or deserialize.

knownTypes
IEnumerable<Type>

An IEnumerable<T> of Type that contains the known types that may be present in the object graph.

maxItemsInObjectGraph
Int32

The maximum number of items in the graph to serialize or deserialize.

ignoreExtensionDataObject
Boolean

true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.

preserveObjectReferences
Boolean

true to use non-standard XML constructs to preserve object reference data; otherwise, false.

dataContractSurrogate
IDataContractSurrogate

An implementation of the IDataContractSurrogate to customize the serialization process.

dataContractResolver
DataContractResolver

An implementation of the DataContractResolver to map xsi:type declarations to data contract types.

Applies to

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

Initializes a new instance of the DataContractSerializer 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.

public:
 DataContractSerializer(Type ^ type, System::String ^ rootName, System::String ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate, System::Runtime::Serialization::DataContractResolver ^ dataContractResolver);
public DataContractSerializer (Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate, System.Runtime.Serialization.DataContractResolver dataContractResolver);
new System.Runtime.Serialization.DataContractSerializer : Type * string * string * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate * System.Runtime.Serialization.DataContractResolver -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As String, rootNamespace As String, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate, dataContractResolver As DataContractResolver)

Parameters

type
Type

The type of the instances that are serialized or deserialized.

rootName
String

The XML element that encloses the content to serialize or deserialize.

rootNamespace
String

The namespace of the XML element that encloses the content to serialize or deserialize.

knownTypes
IEnumerable<Type>

An IEnumerable<T> of Type that contains the known types that may be present in the object graph.

maxItemsInObjectGraph
Int32

The maximum number of items in the graph to serialize or deserialize.

ignoreExtensionDataObject
Boolean

true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.

preserveObjectReferences
Boolean

true to use non-standard XML constructs to preserve object reference data; otherwise, false.

dataContractSurrogate
IDataContractSurrogate

An implementation of the IDataContractSurrogate to customize the serialization process.

dataContractResolver
DataContractResolver

An implementation of the DataContractResolver to map xsi:type declarations to data contract types.

Applies to

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

Initializes a new instance of the DataContractSerializer 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 XmlDictionaryString that specify the XML element and namespace that contain the content.

public:
 DataContractSerializer(Type ^ type, System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate)

Parameters

type
Type

The type of the instances that are serialized or deserialized.

rootName
XmlDictionaryString

The XmlDictionaryString that specifies the XML element that encloses the content to serialize or deserialize.

rootNamespace
XmlDictionaryString

The XmlDictionaryString that specifies the XML namespace of the root.

knownTypes
IEnumerable<Type>

A IEnumerable<T> of Type that contains the known types that may be present in the object graph.

maxItemsInObjectGraph
Int32

The maximum number of items in the graph to serialize or deserialize.

ignoreExtensionDataObject
Boolean

true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.

preserveObjectReferences
Boolean

true to use non-standard XML constructs to preserve object reference data; otherwise, false.

dataContractSurrogate
IDataContractSurrogate

An implementation of the IDataContractSurrogate to customize the serialization process.

Exceptions

The number of items exceeds the maximum value.

Examples

The following example creates an instance of the DataContractSerializer that specifies the type to serialize or deserialize, the root XML element and namespace (as XmlDictionaryString parameters), and an instance of a IEnumerable<T> that contains the types used during deserialization. The code also sets the ignoreExtensionDataObject and preserveObjectReferences parameters to true, and specifies an implementation of the IDataContractSurrogate interface to handle legacy types (types that do not have the DataContractAttribute attribute applied). For more information, see the IDataContractSurrogate documentation.

public static void Constructor9()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create an XmlDictionary and add values to it.
    XmlDictionary d = new XmlDictionary();
    XmlDictionaryString name_value = d.Add("Customer");
    XmlDictionaryString ns_value = d.Add("http://www.contoso.com");

    // Create an instance of a class that
    // implements the IDataContractSurrogate interface.
    // The implementation code is not shown here.
    DCSurrogate mySurrogate = new DCSurrogate();

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        name_value,
        ns_value,
        knownTypeList,
        64 * 1024,
        true,
        true,
        mySurrogate);

    // Other code not shown.
}
Public Shared Sub Constructor9() 

    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))
    
    ' Create an XmlDictionary and add values to it.
    Dim d As New XmlDictionary()
    Dim name_value As XmlDictionaryString = d.Add("Customer")
    Dim ns_value As XmlDictionaryString = d.Add("http://www.contoso.com")
    
    ' Create an instance of a class that 
    ' implements the IDataContractSurrogate interface.
    ' The implementation code is not shown here.
    Dim mySurrogate As New DCSurrogate()
    
    Dim ser As New DataContractSerializer(GetType(Person), _
    name_value, _
    ns_value, _
    knownTypeList, _
    64 * 1064, _
    True, _
    True, _
    mySurrogate)

    ' Other code not shown.    

End Sub

See also

Applies to

DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

Initializes a new instance of the DataContractSerializer 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.

public:
 DataContractSerializer(Type ^ type, System::String ^ rootName, System::String ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate);
public DataContractSerializer (Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate);
new System.Runtime.Serialization.DataContractSerializer : Type * string * string * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As String, rootNamespace As String, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate)

Parameters

type
Type

The type of the instances that are serialized or deserialized.

rootName
String

The XML element that encloses the content to serialize or deserialize.

rootNamespace
String

The namespace of the XML element that encloses the content to serialize or deserialize.

knownTypes
IEnumerable<Type>

An IEnumerable<T> of Type that contains the known types that may be present in the object graph.

maxItemsInObjectGraph
Int32

The maximum number of items in the graph to serialize or deserialize.

ignoreExtensionDataObject
Boolean

true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.

preserveObjectReferences
Boolean

true to use non-standard XML constructs to preserve object reference data; otherwise, false.

dataContractSurrogate
IDataContractSurrogate

An implementation of the IDataContractSurrogate to customize the serialization process.

Exceptions

The number of items exceeds the maximum value.

Examples

The following example creates an instance of the DataContractSerializer that specifies the type to serialize or deserialize, the root XML element and namespace, and an instance of a IEnumerable<T> that contains the types used during deserialization. The code also sets the ignoreExtensionDataObject and preserveObjectReferences parameters to true, and specifies an implementation of the IDataContractSurrogate interface to handle legacy types (types that do not have the DataContractAttribute attribute applied). For more information, see the IDataContractSurrogate documentation.

public static void Constructor8()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create an instance of a class that
    // implements the IDataContractSurrogate interface.
    // The implementation code is not shown here.
    DCSurrogate mySurrogate = new DCSurrogate();

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        "Customer",
        @"http://www.contoso.com",
        knownTypeList,
        64 * 1024,
        true,
        true,
        mySurrogate);

    // Other code not shown.
}
Public Shared Sub Constructor8() 

    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))

    ' Create an instance of a class that 
    ' implements the IDataContractSurrogate interface.
    ' The implementation code is not shown here.
    Dim mySurrogate As New DCSurrogate()

    Dim ser As New DataContractSerializer(GetType(Person), _
    "Customer", _
    "http://www.contoso.com", _
    knownTypeList, _
    64 * 1064, _
    True, _
    True, _
    mySurrogate)

    ' Other code not shown.
End Sub

See also

Applies to

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)

Initializes a new instance of the DataContractSerializer 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.

public:
 DataContractSerializer(Type ^ type, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate);
public DataContractSerializer (Type type, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate);
new System.Runtime.Serialization.DataContractSerializer : Type * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate)

Parameters

type
Type

The type of the instances that are serialized or deserialized.

knownTypes
IEnumerable<Type>

An IEnumerable<T> of Type that contains the known types that may be present in the object graph.

maxItemsInObjectGraph
Int32

The maximum number of items in the graph to serialize or deserialize. The default is the value returned by the Int32.MaxValue property.

ignoreExtensionDataObject
Boolean

true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.

preserveObjectReferences
Boolean

true to use non-standard XML constructs to preserve object reference data; otherwise, false.

dataContractSurrogate
IDataContractSurrogate

An implementation of the IDataContractSurrogate to customize the serialization process.

Exceptions

The number of items exceeds the maximum value.

Examples

The following example creates an instance of the DataContractSerializer that specifies the type to serialize or deserialize and an instance of a IEnumerable<T> to contain the known types used during serialization or deserialization. The code also sets the ignoreExtensionDataObject and preserveObjectReferences parameters to true, and specifies an implementation of the IDataContractSurrogate interface to handle legacy types (types that do not have the DataContractAttribute attribute applied). For more information, see the IDataContractSurrogate documentation.

public static void Constructor7()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create an instance of a class that
    // implements the IDataContractSurrogate interface.
    // The implementation code is not shown here.
    DCSurrogate mySurrogate = new DCSurrogate();

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        knownTypeList,
        64 * 1024,
        true,
        true,
        mySurrogate);
    // Other code not shown.
}
Public Shared Sub Constructor7() 

    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))

    ' Create an instance of a class that 
    ' implements the IDataContractSurrogate interface.
    ' The implementation code is not shown here.
    Dim mySurrogate As New DCSurrogate()

    Dim ser As New DataContractSerializer(GetType(Person), _
    knownTypeList, _
    64 * 1064, _
    True, _
    True, _
     mySurrogate)

    ' Other code not shown.
End Sub

See also

Applies to

DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)

Initializes a new instance of the DataContractSerializer 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.

public:
 DataContractSerializer(Type ^ type, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System::Runtime::Serialization::IDataContractSurrogate ^ dataContractSurrogate, System::Runtime::Serialization::DataContractResolver ^ dataContractResolver);
public DataContractSerializer (Type type, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate, System.Runtime.Serialization.DataContractResolver dataContractResolver);
new System.Runtime.Serialization.DataContractSerializer : Type * seq<Type> * int * bool * bool * System.Runtime.Serialization.IDataContractSurrogate * System.Runtime.Serialization.DataContractResolver -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, knownTypes As IEnumerable(Of Type), maxItemsInObjectGraph As Integer, ignoreExtensionDataObject As Boolean, preserveObjectReferences As Boolean, dataContractSurrogate As IDataContractSurrogate, dataContractResolver As DataContractResolver)

Parameters

type
Type

The type of the instances that are serialized or deserialized.

knownTypes
IEnumerable<Type>

An IEnumerable<T> of Type that contains the known types that may be present in the object graph.

maxItemsInObjectGraph
Int32

The maximum number of items in the graph to serialize or deserialize. The default is the value returned by the Int32.MaxValue property.

ignoreExtensionDataObject
Boolean

true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.

preserveObjectReferences
Boolean

true to use non-standard XML constructs to preserve object reference data; otherwise, false.

dataContractSurrogate
IDataContractSurrogate

An implementation of the IDataContractSurrogate to customize the serialization process.

dataContractResolver
DataContractResolver

An implementation of the DataContractResolver to map xsi:type declarations to data contract types.

Remarks

Warning

This method is not available in Windows Store apps.

Applies to

DataContractSerializer(Type, String, String, IEnumerable<Type>)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

Initializes a new instance of the 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 object graph.

public:
 DataContractSerializer(Type ^ type, System::String ^ rootName, System::String ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes);
public DataContractSerializer (Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes);
public DataContractSerializer (Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type>? knownTypes);
new System.Runtime.Serialization.DataContractSerializer : Type * string * string * seq<Type> -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As String, rootNamespace As String, knownTypes As IEnumerable(Of Type))

Parameters

type
Type

The type of the instances that are serialized or deserialized.

rootName
String

The root element name of the content.

rootNamespace
String

The namespace of the root element.

knownTypes
IEnumerable<Type>

An IEnumerable<T> of Type that contains the types that may be present in the object graph.

Examples

The following example creates an instance of the DataContractSerializer that specifies the type to serialize or deserialize as well as the XML name and namespace to read from or write to the XML document. The code also creates an instance of a IEnumerable<T> to contain the known types used during serialization or deserialization.

public static void Constructor5()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        "Customer",
        @"http://www.contoso.com",
        knownTypeList);

    // Other code not shown.
}
Public Shared Sub Constructor5() 

    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))
    
    Dim ser As New DataContractSerializer(GetType(Person), _
    "Customer", _
    "http://www.contoso.com", _
    knownTypeList)

    ' Other code not shown.

End Sub

See also

Applies to

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

Initializes a new instance of the 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 XmlDictionaryString.

public:
 DataContractSerializer(Type ^ type, System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString)

Parameters

type
Type

The type of the instances that are serialized or deserialized.

rootName
XmlDictionaryString

An XmlDictionaryString that contains the root element name of the content.

rootNamespace
XmlDictionaryString

An XmlDictionaryString that contains the namespace of the root element.

Examples

The following example creates an instance of the DataContractSerializer that specifies the type to serialize or deserialize as well as the XML name and namespace (as XmlDictionaryString objects) to read from or write to the XML document.

public static void Constructor4()
{
    // Create an instance of the DataContractSerializer
    // specifying the type, and name and
    // namespace as XmlDictionaryString objects.

    // Create an XmlDictionary and add values to it.
    XmlDictionary d = new XmlDictionary();
    XmlDictionaryString name_value = d.Add("Customer");
    XmlDictionaryString ns_value = d.Add("http://www.contoso.com");

    // Create the serializer.
    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        name_value,
        ns_value);
    // Other code not shown.
}
Public Shared Sub Constructor4() 
    ' Create an instance of the DataContractSerializer
    ' specifying the type, and name and 
    ' namespace as XmlDictionaryString objects.
    ' Create an XmlDictionary and add values to it.
    Dim d As New XmlDictionary()
    Dim name_value As XmlDictionaryString = d.Add("Customer")
    Dim ns_value As XmlDictionaryString = d.Add("http://www.contoso.com")
    
    ' Create the serializer.
    Dim ser As New DataContractSerializer(GetType(Person), _
    name_value, _
    ns_value)

    ' Other code not shown.
End Sub

Applies to

DataContractSerializer(Type, String, String)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type using the supplied XML root element and namespace.

public:
 DataContractSerializer(Type ^ type, System::String ^ rootName, System::String ^ rootNamespace);
public DataContractSerializer (Type type, string rootName, string rootNamespace);
new System.Runtime.Serialization.DataContractSerializer : Type * string * string -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As String, rootNamespace As String)

Parameters

type
Type

The type of the instances that are serialized or deserialized.

rootName
String

The name of the XML element that encloses the content to serialize or deserialize.

rootNamespace
String

The namespace of the XML element that encloses the content to serialize or deserialize.

Examples

The following example creates an instance of the DataContractSerializer that specifies the type to serialize or deserialize as well as the XML name and namespace to read from or write to the XML document.

public static void Constructor3()
{
    // Create an instance of the DataContractSerializer
    // specifying the type, and name and
    // namespace as strings.
    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        "Customer",
        "http://www.contoso.com");

    // Other code not shown.
}
Public Shared Sub Constructor3() 
    ' Create an instance of the DataContractSerializer
    ' specifying the type, and name and 
    ' namespace as strings.
    Dim ser As New DataContractSerializer(GetType(Person), _
    "Customer", _
    "http://www.contoso.com")

    ' Other code not shown.
End Sub

Applies to

DataContractSerializer(Type, DataContractSerializerSettings)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type and settings.

public:
 DataContractSerializer(Type ^ type, System::Runtime::Serialization::DataContractSerializerSettings ^ settings);
public DataContractSerializer (Type type, System.Runtime.Serialization.DataContractSerializerSettings settings);
public DataContractSerializer (Type type, System.Runtime.Serialization.DataContractSerializerSettings? settings);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Runtime.Serialization.DataContractSerializerSettings -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, settings As DataContractSerializerSettings)

Parameters

type
Type

The type of the instance to serialize or deserialize.

settings
DataContractSerializerSettings

The serializer settings.

Applies to

DataContractSerializer(Type, IEnumerable<Type>)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

Initializes a new instance of the DataContractSerializer 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.

public:
 DataContractSerializer(Type ^ type, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes);
public DataContractSerializer (Type type, System.Collections.Generic.IEnumerable<Type> knownTypes);
public DataContractSerializer (Type type, System.Collections.Generic.IEnumerable<Type>? knownTypes);
new System.Runtime.Serialization.DataContractSerializer : Type * seq<Type> -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, knownTypes As IEnumerable(Of Type))

Parameters

type
Type

The type of the instances that are serialized or deserialized.

knownTypes
IEnumerable<Type>

An IEnumerable<T> of Type that contains the types that may be present in the object graph.

Examples

The following example creates an instance of the DataContractSerializer that specifies the type to serialize or deserialize and a collection of known types that can be used in the object graph.

public static void Constructor2()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create a DatatContractSerializer with the collection.
    DataContractSerializer ser2 = new DataContractSerializer(
        typeof(Orders), knownTypeList);

    // Other code not shown.
}
 Public Shared Sub Constructor2() 

     ' Create a generic List of types and add the known types
     ' to the collection.
     Dim knownTypeList As New List(Of Type)
     knownTypeList.Add(GetType(PurchaseOrder))
     knownTypeList.Add(GetType(PurchaseOrderV3))
     
     ' Create a DatatContractSerializer with the collection.
     Dim ser2 As New DataContractSerializer(GetType(Orders), knownTypeList)

     ' Other code not shown.
End Sub

See also

Applies to

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs

Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. This method also specifies the root XML element and namespace in two XmlDictionaryString parameters as well as a list of known types that may be present in the object graph.

public:
 DataContractSerializer(Type ^ type, System::Xml::XmlDictionaryString ^ rootName, System::Xml::XmlDictionaryString ^ rootNamespace, System::Collections::Generic::IEnumerable<Type ^> ^ knownTypes);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes);
public DataContractSerializer (Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type>? knownTypes);
new System.Runtime.Serialization.DataContractSerializer : Type * System.Xml.XmlDictionaryString * System.Xml.XmlDictionaryString * seq<Type> -> System.Runtime.Serialization.DataContractSerializer
Public Sub New (type As Type, rootName As XmlDictionaryString, rootNamespace As XmlDictionaryString, knownTypes As IEnumerable(Of Type))

Parameters

type
Type

The type of the instances that are serialized or deserialized.

rootName
XmlDictionaryString

An XmlDictionaryString that contains the root element name of the content.

rootNamespace
XmlDictionaryString

An XmlDictionaryString that contains the namespace of the root element.

knownTypes
IEnumerable<Type>

A IEnumerable<T> of Type that contains the known types that may be present in the object graph.

Examples

The following example creates an instance of the DataContractSerializer that specifies the type to serialize or deserialize as well as the XML name and namespace (as XmlDictionaryString objects) to read from or write to the XML document. The code also creates an instance of a IEnumerable<T> to contain the known types used during serialization or deserialization.

public static void Constructor6()
{
    // Create a generic List of types and add the known types
    // to the collection.
    List<Type> knownTypeList = new List<Type>();
    knownTypeList.Add(typeof(PurchaseOrder));
    knownTypeList.Add(typeof(PurchaseOrderV3));

    // Create an XmlDictionary and add values to it.
    XmlDictionary d = new XmlDictionary();
    XmlDictionaryString name_value = d.Add("Customer");
    XmlDictionaryString ns_value = d.Add("http://www.contoso.com");

    DataContractSerializer ser =
        new DataContractSerializer(
        typeof(Person),
        name_value,
        ns_value,
        knownTypeList);

    // Other code not shown.
}
Public Shared Sub Constructor6() 
    ' Create a generic List of types and add the known types
    ' to the collection.
    Dim knownTypeList As New List(Of Type)
    knownTypeList.Add(GetType(PurchaseOrder))
    knownTypeList.Add(GetType(PurchaseOrderV3))

    ' Create an XmlDictionary and add values to it.
    Dim d As New XmlDictionary()
    Dim name_value As XmlDictionaryString = d.Add("Customer")
    Dim ns_value As XmlDictionaryString = d.Add("http://www.contoso.com")
    
    Dim ser As New DataContractSerializer(GetType(Person), _
    name_value, _
    ns_value, _
    knownTypeList)

    ' Other code not shown.
 End Sub

See also

Applies to