DataContractSerializer 构造函数

定义

初始化 DataContractSerializer 类的新实例。

重载

名称 说明
DataContractSerializer(Type)

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定对象图中可能存在的已知类型列表、要序列化的最大图形项数、忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的 XmlDictionaryString 代理项、指定 XML 元素和包含内容的命名空间的参数、 以及运行时映射 xsi:type 声明的替代方法。

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定可能存在于对象图中的已知类型列表、要序列化的最大图形项数、用于忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的代理项、包含内容的 XML 元素和命名空间, 以及运行时映射 xsi:type 声明的替代方法。

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定要存在于对象图中的已知类型的列表、要序列化的最大图形项数、用于忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的 XmlDictionaryString 代理项以及指定包含内容的 XML 元素和命名空间的参数。

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定要存在于对象图中的已知类型列表、要序列化的最大图形项数、用于忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的代理项以及包含内容的 XML 元素和命名空间。

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定要存在于对象图中的已知类型列表、要序列化的最大图形项数、忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据,以及自定义序列化的代理项。

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定要存在于对象图中的已知类型列表、要序列化的最大图形项数、用于忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的代理项,以及运行时映射 xsi:type 声明的替代项。

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定两个字符串参数中的根 XML 元素和命名空间,以及对象图中可能存在的已知类型的列表。

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)

初始化类的新实例 DataContractSerializer ,以使用通过类型的参数指定的 XML 根元素和命名空间序列化或反序列化指定类型的 XmlDictionaryString对象。

DataContractSerializer(Type, String, String)

初始化类的新实例 DataContractSerializer ,以使用提供的 XML 根元素和命名空间序列化或反序列化指定类型的对象。

DataContractSerializer(Type, DataContractSerializerSettings)

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型和设置的对象。

DataContractSerializer(Type, IEnumerable<Type>)

初始化类的新实例 DataContractSerializer 以序列化或反序列化指定类型的对象,以及对象图中可能存在的已知类型的集合。

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定两 XmlDictionaryString 个参数中的根 XML 元素和命名空间,以及可能存在于对象图中的已知类型的列表。

DataContractSerializer(Type)

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。

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

参数

type
Type

序列化或反序列化的实例的类型。

示例

以下示例创建一个实例,该实例 DataContractSerializer 指定要序列化或反序列化的类型。

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

适用于

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定对象图中可能存在的已知类型列表、要序列化的最大图形项数、忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的 XmlDictionaryString 代理项、指定 XML 元素和包含内容的命名空间的参数、 以及运行时映射 xsi:type 声明的替代方法。

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)

参数

type
Type

序列化或反序列化的实例的类型。

rootName
XmlDictionaryString

包含要序列化或反序列化的内容的 XML 元素。

rootNamespace
XmlDictionaryString

包含要序列化或反序列化的内容的 XML 元素的命名空间。

knownTypes
IEnumerable<Type>

IEnumerable<T>其中一个Type类型包含对象图中可能存在的已知类型。

maxItemsInObjectGraph
Int32

要序列化或反序列化的图形中项的最大数目。

ignoreExtensionDataObject
Boolean

true (如果忽略序列化和反序列化时类型扩展提供的数据);否则,为 false.

preserveObjectReferences
Boolean

true 若要使用非标准 XML 构造来保留对象引用数据,则为 ;否则,为 false.

dataContractSurrogate
IDataContractSurrogate

用于自定义序列化过程的实现 IDataContractSurrogate

dataContractResolver
DataContractResolver

将声明映射到DataContractResolver数据协定类型的实现xsi:type

适用于

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定可能存在于对象图中的已知类型列表、要序列化的最大图形项数、用于忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的代理项、包含内容的 XML 元素和命名空间, 以及运行时映射 xsi:type 声明的替代方法。

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)

参数

type
Type

序列化或反序列化的实例的类型。

rootName
String

包含要序列化或反序列化的内容的 XML 元素。

rootNamespace
String

包含要序列化或反序列化的内容的 XML 元素的命名空间。

knownTypes
IEnumerable<Type>

IEnumerable<T>其中一个Type类型包含对象图中可能存在的已知类型。

maxItemsInObjectGraph
Int32

要序列化或反序列化的图形中项的最大数目。

ignoreExtensionDataObject
Boolean

true (如果忽略序列化和反序列化时类型扩展提供的数据);否则,为 false.

preserveObjectReferences
Boolean

true 若要使用非标准 XML 构造来保留对象引用数据,则为 ;否则,为 false.

dataContractSurrogate
IDataContractSurrogate

用于自定义序列化过程的实现 IDataContractSurrogate

dataContractResolver
DataContractResolver

将声明映射到DataContractResolver数据协定类型的实现xsi:type

适用于

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定要存在于对象图中的已知类型的列表、要序列化的最大图形项数、用于忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的 XmlDictionaryString 代理项以及指定包含内容的 XML 元素和命名空间的参数。

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)

参数

type
Type

序列化或反序列化的实例的类型。

rootName
XmlDictionaryString

指定 XmlDictionaryString 包含要序列化或反序列化的内容的 XML 元素。

rootNamespace
XmlDictionaryString

指定 XmlDictionaryString 根的 XML 命名空间。

knownTypes
IEnumerable<Type>

IEnumerable<T>其中一个Type类型包含对象图中可能存在的已知类型。

maxItemsInObjectGraph
Int32

要序列化或反序列化的图形中项的最大数目。

ignoreExtensionDataObject
Boolean

true (如果忽略序列化和反序列化时类型扩展提供的数据);否则,为 false.

preserveObjectReferences
Boolean

true 若要使用非标准 XML 构造来保留对象引用数据,则为 ;否则,为 false.

dataContractSurrogate
IDataContractSurrogate

用于自定义序列化过程的实现 IDataContractSurrogate

例外

项数超过最大值。

示例

以下示例创建一个实例,该实例 DataContractSerializer 指定要序列化或反序列化的类型、根 XML 元素和命名空间(作为 XmlDictionaryString 参数),以及包含反序列化期间使用的类型的 IEnumerable<T> 实例。 该代码还将设置ignoreExtensionDataObject和参数设置为preserveObjectReferences,并指定接口的true实现来处理旧类型(未IDataContractSurrogateDataContractAttribute应用属性的类型)。 有关详细信息,请参阅 IDataContractSurrogate 文档。

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

另请参阅

适用于

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定要存在于对象图中的已知类型列表、要序列化的最大图形项数、用于忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的代理项以及包含内容的 XML 元素和命名空间。

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)

参数

type
Type

序列化或反序列化的实例的类型。

rootName
String

包含要序列化或反序列化的内容的 XML 元素。

rootNamespace
String

包含要序列化或反序列化的内容的 XML 元素的命名空间。

knownTypes
IEnumerable<Type>

IEnumerable<T>其中一个Type类型包含对象图中可能存在的已知类型。

maxItemsInObjectGraph
Int32

要序列化或反序列化的图形中项的最大数目。

ignoreExtensionDataObject
Boolean

true (如果忽略序列化和反序列化时类型扩展提供的数据);否则,为 false.

preserveObjectReferences
Boolean

true 若要使用非标准 XML 构造来保留对象引用数据,则为 ;否则,为 false.

dataContractSurrogate
IDataContractSurrogate

用于自定义序列化过程的实现 IDataContractSurrogate

例外

项数超过最大值。

示例

以下示例创建一个实例,该实例 DataContractSerializer 指定要序列化或反序列化的类型、根 XML 元素和命名空间,以及包含反序列化期间使用的类型的 IEnumerable<T> 实例。 该代码还将设置ignoreExtensionDataObject和参数设置为preserveObjectReferences,并指定接口的true实现来处理旧类型(未IDataContractSurrogateDataContractAttribute应用属性的类型)。 有关详细信息,请参阅 IDataContractSurrogate 文档。

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

另请参阅

适用于

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定要存在于对象图中的已知类型列表、要序列化的最大图形项数、忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据,以及自定义序列化的代理项。

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)

参数

type
Type

序列化或反序列化的实例的类型。

knownTypes
IEnumerable<Type>

IEnumerable<T>其中一个Type类型包含对象图中可能存在的已知类型。

maxItemsInObjectGraph
Int32

要序列化或反序列化的图形中项的最大数目。 默认值为 Int32.MaxValue 属性返回的值。

ignoreExtensionDataObject
Boolean

true (如果忽略序列化和反序列化时类型扩展提供的数据);否则,为 false.

preserveObjectReferences
Boolean

true 若要使用非标准 XML 构造来保留对象引用数据,则为 ;否则,为 false.

dataContractSurrogate
IDataContractSurrogate

用于自定义序列化过程的实现 IDataContractSurrogate

例外

项数超过最大值。

示例

以下示例创建一个实例,该实例 DataContractSerializer 指定要序列化或反序列化的类型,以及包含序列化或反序列化期间使用的已知类型的实例 IEnumerable<T> 。 该代码还将设置ignoreExtensionDataObject和参数设置为preserveObjectReferences,并指定接口的true实现来处理旧类型(未IDataContractSurrogateDataContractAttribute应用属性的类型)。 有关详细信息,请参阅 IDataContractSurrogate 文档。

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

另请参阅

适用于

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定要存在于对象图中的已知类型列表、要序列化的最大图形项数、用于忽略意外数据的参数、是否使用非标准 XML 构造来保留图形中的对象引用数据、自定义序列化的代理项,以及运行时映射 xsi:type 声明的替代项。

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)

参数

type
Type

序列化或反序列化的实例的类型。

knownTypes
IEnumerable<Type>

IEnumerable<T>其中一个Type类型包含对象图中可能存在的已知类型。

maxItemsInObjectGraph
Int32

要序列化或反序列化的图形中项的最大数目。 默认值为 Int32.MaxValue 属性返回的值。

ignoreExtensionDataObject
Boolean

true (如果忽略序列化和反序列化时类型扩展提供的数据);否则,为 false.

preserveObjectReferences
Boolean

true 若要使用非标准 XML 构造来保留对象引用数据,则为 ;否则,为 false.

dataContractSurrogate
IDataContractSurrogate

用于自定义序列化过程的实现 IDataContractSurrogate

dataContractResolver
DataContractResolver

将声明映射到DataContractResolver数据协定类型的实现xsi:type

注解

Warning

此方法在 Windows 应用商店应用中不可用。

适用于

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

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定两个字符串参数中的根 XML 元素和命名空间,以及对象图中可能存在的已知类型的列表。

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

参数

type
Type

序列化或反序列化的实例的类型。

rootName
String

内容的根元素名称。

rootNamespace
String

根元素的命名空间。

knownTypes
IEnumerable<Type>

IEnumerable<T>其中一个Type包含对象图中可能存在的类型。

示例

以下示例创建一个实例,该实例 DataContractSerializer 指定要序列化或反序列化的类型以及要从 XML 文档读取或写入的 XML 名称和命名空间。 该代码还会创建一个实例 IEnumerable<T> ,以包含序列化或反序列化期间使用的已知类型。

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

另请参阅

适用于

DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)

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

初始化类的新实例 DataContractSerializer ,以使用通过类型的参数指定的 XML 根元素和命名空间序列化或反序列化指定类型的 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)

参数

type
Type

序列化或反序列化的实例的类型。

rootName
XmlDictionaryString

一个 XmlDictionaryString 包含内容的根元素名称。

rootNamespace
XmlDictionaryString

一个 XmlDictionaryString 包含根元素的命名空间。

示例

以下示例创建一个实例,该实例 DataContractSerializer 指定要序列化或反序列化的类型,以及要从 XML 文档读取或写入的 XML 名称和命名空间(作为 XmlDictionaryString 对象)。

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

适用于

DataContractSerializer(Type, String, String)

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

初始化类的新实例 DataContractSerializer ,以使用提供的 XML 根元素和命名空间序列化或反序列化指定类型的对象。

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)

参数

type
Type

序列化或反序列化的实例的类型。

rootName
String

包含要序列化或反序列化的内容的 XML 元素的名称。

rootNamespace
String

包含要序列化或反序列化的内容的 XML 元素的命名空间。

示例

以下示例创建一个实例,该实例 DataContractSerializer 指定要序列化或反序列化的类型以及要从 XML 文档读取或写入的 XML 名称和命名空间。

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

适用于

DataContractSerializer(Type, DataContractSerializerSettings)

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型和设置的对象。

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)

参数

type
Type

要序列化或反序列化的实例的类型。

settings
DataContractSerializerSettings

序列化程序设置。

适用于

DataContractSerializer(Type, IEnumerable<Type>)

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

初始化类的新实例 DataContractSerializer 以序列化或反序列化指定类型的对象,以及对象图中可能存在的已知类型的集合。

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

参数

type
Type

序列化或反序列化的实例的类型。

knownTypes
IEnumerable<Type>

IEnumerable<T>其中一个Type包含对象图中可能存在的类型。

示例

以下示例创建一个实例,该实例 DataContractSerializer 指定要序列化或反序列化的类型以及可在对象图中使用的已知类型的集合。

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

另请参阅

适用于

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

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

初始化类的新实例 DataContractSerializer ,以序列化或反序列化指定类型的对象。 此方法还指定两 XmlDictionaryString 个参数中的根 XML 元素和命名空间,以及可能存在于对象图中的已知类型的列表。

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

参数

type
Type

序列化或反序列化的实例的类型。

rootName
XmlDictionaryString

一个 XmlDictionaryString 包含内容的根元素名称。

rootNamespace
XmlDictionaryString

一个 XmlDictionaryString 包含根元素的命名空间。

knownTypes
IEnumerable<Type>

IEnumerable<T>其中一个Type类型包含对象图中可能存在的已知类型。

示例

以下示例创建一个实例,该实例 DataContractSerializer 指定要序列化或反序列化的类型,以及要从 XML 文档读取或写入的 XML 名称和命名空间(作为 XmlDictionaryString 对象)。 该代码还会创建一个实例 IEnumerable<T> ,以包含序列化或反序列化期间使用的已知类型。

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

另请参阅

适用于