DataContractSerializer 构造函数
定义
初始化 DataContractSerializer 类的新实例。Initializes a new instance of the DataContractSerializer class.
重载
| DataContractSerializer(Type) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。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) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、要忽略意外数据的参数、是否使用非标准的 XML 构造来保留图中的对象引用数据、自定义序列化的代理项、指定包含内容的 XML 元素和命名空间的 XmlDictionaryString 参数以及在运行时映射 |
| DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、要忽略意外数据的参数、是否使用非标准的 XML 构造来保留在图中的对象引用数据、自定义序列化的代理项、包含内容的 XML 元素和命名空间以及在运行时映射 |
| DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、要序列化的最大图项数、指定忽略意外数据的参数、是否使用非标准的 XML 结构来保留对象引用数据、自定义序列化的代理项以及指定包含内容的 XML 元素和命名空间的 XmlDictionaryString 参数。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) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、指定忽略意外数据的参数、是否使用非标准的 XML 结构来保留对象引用数据、自定义序列化的代理项以及包含内容的 XML 元素和命名空间。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) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、指定忽略意外数据的参数、是否使用非标准的 XML 结构来保留对象引用数据以及自定义序列化的代理项。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) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、要忽略意外数据的参数、是否使用非标准的 XML 构造来保留图中的对象引用数据、自定义序列化的代理项以及在运行时映射 |
| DataContractSerializer(Type, String, String, IEnumerable<Type>) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还在两个字符串参数中指定根 XML 元素和命名空间,并指定了可在对象图中呈现的已知类型的列表。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) |
使用通过类型为 DataContractSerializer 的参数指定的 XML 根元素和命名空间初始化 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) |
使用提供的 XML 根元素和命名空间初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。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) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型和设置的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type and settings. |
| DataContractSerializer(Type, IEnumerable<Type>) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象以及可在对象图中呈现的已知类型的集合。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>) |
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还在两个 XmlDictionaryString 参数中指定根 XML 元素和命名空间,并指定了可在对象图中呈现的已知类型的列表。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)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。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)
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
示例
下面的示例创建一个 DataContractSerializer 实例,以便指定要序列化或反序列化的类型。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
适用于
DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、要忽略意外数据的参数、是否使用非标准的 XML 构造来保留图中的对象引用数据、自定义序列化的代理项、指定包含内容的 XML 元素和命名空间的 XmlDictionaryString 参数以及在运行时映射 xsi: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)
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- rootName
- XmlDictionaryString
包含要序列化或反序列化的内容的 XML 元素。The XML element that encloses the content to serialize or deserialize.
- rootNamespace
- XmlDictionaryString
XML 元素的命名空间,该元素包含要序列化或反序列化的内容。The namespace of the XML element that encloses the content to serialize or deserialize.
- knownTypes
- IEnumerable<Type>
IEnumerable<T> 的一个 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;否则为 false。true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
- preserveObjectReferences
- Boolean
要使用非标准的 XML 结构来保留对象引用数据,则为 true;否则为 false。true to use non-standard XML constructs to preserve object reference data; otherwise, false.
- dataContractSurrogate
- IDataContractSurrogate
一个用于自定义序列化过程的 IDataContractSurrogate 实现。An implementation of the IDataContractSurrogate to customize the serialization process.
- dataContractResolver
- DataContractResolver
用于将 xsi:type 声明映射到数据协定类型的 DataContractResolver 的实现。An implementation of the DataContractResolver to map xsi:type declarations to data contract types.
适用于
DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、要忽略意外数据的参数、是否使用非标准的 XML 构造来保留在图中的对象引用数据、自定义序列化的代理项、包含内容的 XML 元素和命名空间以及在运行时映射 xsi: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)
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- rootName
- String
包含要序列化或反序列化的内容的 XML 元素。The XML element that encloses the content to serialize or deserialize.
- rootNamespace
- String
XML 元素的命名空间,该元素包含要序列化或反序列化的内容。The namespace of the XML element that encloses the content to serialize or deserialize.
- knownTypes
- IEnumerable<Type>
IEnumerable<T> 的一个 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;否则为 false。true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
- preserveObjectReferences
- Boolean
要使用非标准的 XML 结构来保留对象引用数据,则为 true;否则为 false。true to use non-standard XML constructs to preserve object reference data; otherwise, false.
- dataContractSurrogate
- IDataContractSurrogate
一个用于自定义序列化过程的 IDataContractSurrogate 实现。An implementation of the IDataContractSurrogate to customize the serialization process.
- dataContractResolver
- DataContractResolver
用于将 xsi:type 声明映射到数据协定类型的 DataContractResolver 的实现。An implementation of the DataContractResolver to map xsi:type declarations to data contract types.
适用于
DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、要序列化的最大图项数、指定忽略意外数据的参数、是否使用非标准的 XML 结构来保留对象引用数据、自定义序列化的代理项以及指定包含内容的 XML 元素和命名空间的 XmlDictionaryString 参数。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)
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- rootName
- XmlDictionaryString
指定包含要序列化或反序列化的内容的 XML 元素的 XmlDictionaryString。The XmlDictionaryString that specifies the XML element that encloses the content to serialize or deserialize.
- rootNamespace
- XmlDictionaryString
指定根 XML 命名空间的 XmlDictionaryString。The XmlDictionaryString that specifies the XML namespace of the root.
- knownTypes
- IEnumerable<Type>
IEnumerable<T> 的一个 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;否则为 false。true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
- preserveObjectReferences
- Boolean
要使用非标准的 XML 结构来保留对象引用数据,则为 true;否则为 false。true to use non-standard XML constructs to preserve object reference data; otherwise, false.
- dataContractSurrogate
- IDataContractSurrogate
一个用于自定义序列化过程的 IDataContractSurrogate 实现。An implementation of the IDataContractSurrogate to customize the serialization process.
例外
项数超出了最大值。The number of items exceeds the maximum value.
示例
下面的示例创建一个 DataContractSerializer 实例以指定要序列化或反序列化的类型以及根 XML 元素和命名空间(作为 XmlDictionaryString 参数),并且还创建了一个 IEnumerable<T> 实例以包含在反序列化期间使用的类型。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. 此代码还将 ignoreExtensionDataObject 和 preserveObjectReferences 参数设置为 true ,并指定接口的实现 IDataContractSurrogate 来处理 (类型未 DataContractAttribute 应用) 的类型。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). 有关详细信息,请参阅 IDataContractSurrogate 文档。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
另请参阅
- MaxItemsInObjectGraph
- PreserveObjectReferences
- IgnoreExtensionDataObject
- IExtensibleDataObject
- ExtensionDataObject
- IDataContractSurrogate
适用于
DataContractSerializer(Type, String, String, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、指定忽略意外数据的参数、是否使用非标准的 XML 结构来保留对象引用数据、自定义序列化的代理项以及包含内容的 XML 元素和命名空间。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)
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- rootName
- String
包含要序列化或反序列化的内容的 XML 元素。The XML element that encloses the content to serialize or deserialize.
- rootNamespace
- String
XML 元素的命名空间,该元素包含要序列化或反序列化的内容。The namespace of the XML element that encloses the content to serialize or deserialize.
- knownTypes
- IEnumerable<Type>
IEnumerable<T> 的一个 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;否则为 false。true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
- preserveObjectReferences
- Boolean
要使用非标准的 XML 结构来保留对象引用数据,则为 true;否则为 false。true to use non-standard XML constructs to preserve object reference data; otherwise, false.
- dataContractSurrogate
- IDataContractSurrogate
一个用于自定义序列化过程的 IDataContractSurrogate 实现。An implementation of the IDataContractSurrogate to customize the serialization process.
例外
项数超出了最大值。The number of items exceeds the maximum value.
示例
下面的示例创建一个 DataContractSerializer 实例以指定要序列化或反序列化的类型以及根 XML 元素和命名空间,并且还创建了一个 IEnumerable<T> 实例以包含在反序列化期间使用的类型。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. 此代码还将 ignoreExtensionDataObject 和 preserveObjectReferences 参数设置为 true ,并指定接口的实现 IDataContractSurrogate 来处理 (类型未 DataContractAttribute 应用) 的类型。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). 有关详细信息,请参阅 IDataContractSurrogate 文档。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
另请参阅
- MaxItemsInObjectGraph
- PreserveObjectReferences
- IgnoreExtensionDataObject
- IExtensibleDataObject
- ExtensionDataObject
- KnownTypeAttribute
- IDataContractSurrogate
适用于
DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、指定忽略意外数据的参数、是否使用非标准的 XML 结构来保留对象引用数据以及自定义序列化的代理项。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)
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- knownTypes
- IEnumerable<Type>
IEnumerable<T> 的一个 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. 默认值为 MaxValue 属性返回的值。The default is the value returned by the MaxValue property.
- ignoreExtensionDataObject
- Boolean
要在序列化和反序列化时忽略类型扩展提供的数据,则为 true;否则为 false。true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
- preserveObjectReferences
- Boolean
要使用非标准的 XML 结构来保留对象引用数据,则为 true;否则为 false。true to use non-standard XML constructs to preserve object reference data; otherwise, false.
- dataContractSurrogate
- IDataContractSurrogate
一个用于自定义序列化过程的 IDataContractSurrogate 实现。An implementation of the IDataContractSurrogate to customize the serialization process.
例外
项数超出了最大值。The number of items exceeds the maximum value.
示例
下面的示例创建一个 DataContractSerializer 实例以指定要序列化或反序列化的类型,并且还创建了一个 IEnumerable<T> 实例以包含在序列化或反序列化期间使用的已知类型。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. 此代码还将 ignoreExtensionDataObject 和 preserveObjectReferences 参数设置为 true ,并指定接口的实现 IDataContractSurrogate 来处理 (类型未 DataContractAttribute 应用) 的类型。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). 有关详细信息,请参阅 IDataContractSurrogate 文档。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
另请参阅
- MaxItemsInObjectGraph
- PreserveObjectReferences
- IgnoreExtensionDataObject
- IExtensibleDataObject
- ExtensionDataObject
- KnownTypeAttribute
- IDataContractSurrogate
适用于
DataContractSerializer(Type, IEnumerable<Type>, Int32, Boolean, Boolean, IDataContractSurrogate, DataContractResolver)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还指定了可在对象图中呈现的已知类型的列表、图中要序列化的最大项数、要忽略意外数据的参数、是否使用非标准的 XML 构造来保留图中的对象引用数据、自定义序列化的代理项以及在运行时映射 xsi: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)
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- knownTypes
- IEnumerable<Type>
IEnumerable<T> 的一个 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. 默认值为 MaxValue 属性返回的值。The default is the value returned by the MaxValue property.
- ignoreExtensionDataObject
- Boolean
要在序列化和反序列化时忽略类型扩展提供的数据,则为 true;否则为 false。true to ignore the data supplied by an extension of the type upon serialization and deserialization; otherwise, false.
- preserveObjectReferences
- Boolean
要使用非标准的 XML 结构来保留对象引用数据,则为 true;否则为 false。true to use non-standard XML constructs to preserve object reference data; otherwise, false.
- dataContractSurrogate
- IDataContractSurrogate
一个用于自定义序列化过程的 IDataContractSurrogate 实现。An implementation of the IDataContractSurrogate to customize the serialization process.
- dataContractResolver
- DataContractResolver
用于将 xsi:type 声明映射到数据协定类型的 DataContractResolver 的实现。An implementation of the DataContractResolver to map xsi:type declarations to data contract types.
注解
警告
此方法在 Windows 应用商店应用中不可用。This method is not available in Windows Store apps.
适用于
DataContractSerializer(Type, String, String, IEnumerable<Type>)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还在两个字符串参数中指定根 XML 元素和命名空间,并指定了可在对象图中呈现的已知类型的列表。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))
参数
- 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>
IEnumerable<T> 的一个 Type,其中包含可在对象图中呈现的类型。An IEnumerable<T> of Type that contains the types that may be present in the object graph.
示例
下面的示例创建一个 DataContractSerializer 实例,它指定了要序列化或反序列化的类型以及在 XML 文档中读取或写入的 XML 名称和命名空间。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. 该代码还创建一个 IEnumerable<T> 实例,以包含在序列化或反序列化期间使用的已知类型。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
另请参阅
适用于
DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString)
使用通过类型为 DataContractSerializer 的参数指定的 XML 根元素和命名空间初始化 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.
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
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- rootName
- XmlDictionaryString
一个包含内容的根元素名称的 XmlDictionaryString。An XmlDictionaryString that contains the root element name of the content.
- rootNamespace
- XmlDictionaryString
一个包含根元素的命名空间的 XmlDictionaryString。An XmlDictionaryString that contains the namespace of the root element.
示例
下面的示例创建一个 DataContractSerializer 实例,它指定了要序列化或反序列化的类型以及在 XML 文档中读取或写入的 XML 名称和命名空间(作为 XmlDictionaryString 参数)。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
适用于
DataContractSerializer(Type, String, String)
使用提供的 XML 根元素和命名空间初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。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)
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- rootName
- String
XML 元素的名称,该元素包含要序列化或反序列化的内容。The name of the XML element that encloses the content to serialize or deserialize.
- rootNamespace
- String
XML 元素的命名空间,该元素包含要序列化或反序列化的内容。The namespace of the XML element that encloses the content to serialize or deserialize.
示例
下面的示例创建一个 DataContractSerializer 实例,它指定了要序列化或反序列化的类型以及在 XML 文档中读取或写入的 XML 名称和命名空间。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
适用于
DataContractSerializer(Type, DataContractSerializerSettings)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型和设置的对象。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)
参数
- type
- Type
被序列化或反序列化的实例类型。The type of the instance to serialize or deserialize.
- settings
- DataContractSerializerSettings
序列化程序设置。The serializer settings.
适用于
DataContractSerializer(Type, IEnumerable<Type>)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象以及可在对象图中呈现的已知类型的集合。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))
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- knownTypes
- IEnumerable<Type>
IEnumerable<T> 的一个 Type,其中包含可在对象图中呈现的类型。An IEnumerable<T> of Type that contains the types that may be present in the object graph.
示例
下面的示例创建一个 DataContractSerializer 实例,它指定了要序列化或反序列化的类型以及可在对象图中使用的已知类型的集合。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
另请参阅
适用于
DataContractSerializer(Type, XmlDictionaryString, XmlDictionaryString, IEnumerable<Type>)
初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type. 此方法还在两个 XmlDictionaryString 参数中指定根 XML 元素和命名空间,并指定了可在对象图中呈现的已知类型的列表。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))
参数
- type
- Type
序列化或反序列化的实例的类型。The type of the instances that are serialized or deserialized.
- rootName
- XmlDictionaryString
一个包含内容的根元素名称的 XmlDictionaryString。An XmlDictionaryString that contains the root element name of the content.
- rootNamespace
- XmlDictionaryString
一个包含根元素的命名空间的 XmlDictionaryString。An XmlDictionaryString that contains the namespace of the root element.
- knownTypes
- IEnumerable<Type>
IEnumerable<T> 的一个 Type,其中包含可在对象图中呈现的已知类型。A IEnumerable<T> of Type that contains the known types that may be present in the object graph.
示例
下面的示例创建一个 DataContractSerializer 实例,它指定了要序列化或反序列化的类型以及在 XML 文档中读取或写入的 XML 名称和命名空间(作为 XmlDictionaryString 参数)。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. 该代码还创建一个 IEnumerable<T> 实例,以包含在序列化或反序列化期间使用的已知类型。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