Redigera

DataContractSerializer Constructors

Definition

Initializes a new instance of the DataContractSerializer class.

Overloads

Name Description
DataContractSerializer(Type)

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

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, DataContractSerializerSettings)

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

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, 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, 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, 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
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, IEnumerable<Type>)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
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, DataContractSerializerSettings)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
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, String, String)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
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, XmlDictionaryString, XmlDictionaryString)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
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, IEnumerable<Type>)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
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, IEnumerable<Type>)

Source:
DataContractSerializer.cs
Source:
DataContractSerializer.cs
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