Прочетете на английски Редактиране

Споделяне чрез


DataContractSerializer Constructors

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

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.

C#
public DataContractSerializer(Type 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.

C#
public static void Constructor1()
{
    // Create an instance of the DataContractSerializer.
    DataContractSerializer ser =
        new DataContractSerializer(typeof(Person));
    // Other code not shown.
}

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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.

C#
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);

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

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
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);

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

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
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);

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.

C#
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.
}

See also

Applies to

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
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);

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.

C#
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.
}

See also

Applies to

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
public DataContractSerializer(Type type, System.Collections.Generic.IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, System.Runtime.Serialization.IDataContractSurrogate dataContractSurrogate);

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.

C#
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.
}

See also

Applies to

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
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);

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

Предупреждение

This method is not available in Windows Store apps.

Applies to

.NET Framework 4.8.1 и други версии
Продукт Версии
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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.

C#
public DataContractSerializer(Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes);
C#
public DataContractSerializer(Type type, string rootName, string rootNamespace, System.Collections.Generic.IEnumerable<Type>? knownTypes);

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.

C#
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.
}

See also

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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.

C#
public DataContractSerializer(Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace);

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.

C#
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.
}

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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.

C#
public DataContractSerializer(Type type, string rootName, string rootNamespace);

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.

C#
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.
}

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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.

C#
public DataContractSerializer(Type type, System.Runtime.Serialization.DataContractSerializerSettings settings);
C#
public DataContractSerializer(Type type, System.Runtime.Serialization.DataContractSerializerSettings? settings);

Parameters

type
Type

The type of the instance to serialize or deserialize.

settings
DataContractSerializerSettings

The serializer settings.

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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.

C#
public DataContractSerializer(Type type, System.Collections.Generic.IEnumerable<Type> knownTypes);
C#
public DataContractSerializer(Type type, System.Collections.Generic.IEnumerable<Type>? knownTypes);

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.

C#
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.
}

See also

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

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.

C#
public DataContractSerializer(Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type> knownTypes);
C#
public DataContractSerializer(Type type, System.Xml.XmlDictionaryString rootName, System.Xml.XmlDictionaryString rootNamespace, System.Collections.Generic.IEnumerable<Type>? knownTypes);

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.

C#
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.
}

See also

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0