DataContractSerializer 类

定义

使用提供的数据协定,将类型实例序列化和反序列化为 XML 流或文档。Serializes and deserializes an instance of a type into an XML stream or document using a supplied data contract. 此类不能被继承。This class cannot be inherited.

public ref class DataContractSerializer sealed : System::Runtime::Serialization::XmlObjectSerializer
public sealed class DataContractSerializer : System.Runtime.Serialization.XmlObjectSerializer
type DataContractSerializer = class
    inherit XmlObjectSerializer
Public NotInheritable Class DataContractSerializer
Inherits XmlObjectSerializer
继承
DataContractSerializer

示例

下面的示例代码演示一种名为 Person 且由 DataContractSerializer 序列化的类型。The following example code shows a type named Person that is serialized by the DataContractSerializer. 它将 DataContractAttribute 特性应用于类并将 DataMemberAttribute 应用于成员,以便向 DataContractSerializer 指示要序列化的内容。The DataContractAttribute attribute is applied to the class, and the DataMemberAttribute is applied to members to instruct the DataContractSerializer what to serialize.

namespace DataContractSerializerExample
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Xml;

    // You must apply a DataContractAttribute or SerializableAttribute
    // to a class to have it serialized by the DataContractSerializer.
    [DataContract(Name = "Customer", Namespace = "http://www.contoso.com")]
    class Person : IExtensibleDataObject
    {
        [DataMember()]
        public string FirstName;
        [DataMember]
        public string LastName;
        [DataMember()]
        public int ID;

        public Person(string newfName, string newLName, int newID)
        {
            FirstName = newfName;
            LastName = newLName;
            ID = newID;
        }

        private ExtensionDataObject extensionData_Value;

        public ExtensionDataObject ExtensionData
        {
            get
            {
                return extensionData_Value;
            }
            set
            {
                extensionData_Value = value;
            }
        }
    }

    public sealed class Test
    {
        private Test() { }

        public static void Main()
        {
            try
            {
                WriteObject("DataContractSerializerExample.xml");
                ReadObject("DataContractSerializerExample.xml");
            }

            catch (SerializationException serExc)
            {
                Console.WriteLine("Serialization Failed");
                Console.WriteLine(serExc.Message);
            }
            catch (Exception exc)
            {
                Console.WriteLine(
                "The serialization operation failed: {0} StackTrace: {1}",
                exc.Message, exc.StackTrace);
            }

            finally
            {
                Console.WriteLine("Press <Enter> to exit....");
                Console.ReadLine();
            }
        }

        public static void WriteObject(string fileName)
        {
            Console.WriteLine(
                "Creating a Person object and serializing it.");
            Person p1 = new Person("Zighetti", "Barbara", 101);
            FileStream writer = new FileStream(fileName, FileMode.Create);
            DataContractSerializer ser =
                new DataContractSerializer(typeof(Person));
            ser.WriteObject(writer, p1);
            writer.Close();
        }

        public static void ReadObject(string fileName)
        {
            Console.WriteLine("Deserializing an instance of the object.");
            FileStream fs = new FileStream(fileName,
            FileMode.Open);
            XmlDictionaryReader reader =
                XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
            DataContractSerializer ser = new DataContractSerializer(typeof(Person));

            // Deserialize the data and read it from the instance.
            Person deserializedPerson =
                (Person)ser.ReadObject(reader, true);
            reader.Close();
            fs.Close();
            Console.WriteLine(String.Format("{0} {1}, ID: {2}",
            deserializedPerson.FirstName, deserializedPerson.LastName,
            deserializedPerson.ID));
        }
    }
' You must apply a DataContractAttribute or SerializableAttribute
' to a class to have it serialized by the DataContractSerializer.
<DataContract(Name := "Customer", [Namespace] := "http://www.contoso.com")>  _
Class Person
    Implements IExtensibleDataObject
    <DataMember()>  _
    Public FirstName As String
    <DataMember()>  _
    Public LastName As String
    <DataMember()>  _
    Public ID As Integer
    
    Public Sub New(ByVal newfName As String, ByVal newLName As String, ByVal newID As Integer) 
        FirstName = newfName
        LastName = newLName
        ID = newID
    End Sub 
    
    Private extensionData_Value As ExtensionDataObject
    
    Public Property ExtensionData() As ExtensionDataObject Implements _
       IExtensibleDataObject.ExtensionData
        Get
            Return extensionData_Value
        End Get
        Set
            extensionData_Value = value
        End Set
    End Property
End Class 


NotInheritable Public Class Test
    
    Private Sub New() 
    
    End Sub 
     
    Public Shared Sub Main() 
        Try
            WriteObject("DataContractSerializerExample.xml")
            ReadObject("DataContractSerializerExample.xml")
        
        Catch serExc As SerializationException
            Console.WriteLine("Serialization Failed")
            Console.WriteLine(serExc.Message)
        Catch exc As Exception
            Console.WriteLine("The serialization operation failed: {0} StackTrace: {1}", exc.Message, exc.StackTrace)
        
        Finally
            Console.WriteLine("Press <Enter> to exit....")
            Console.ReadLine()
        End Try
    End Sub 
    
    Public Shared Sub WriteObject(ByVal fileName As String) 
        Console.WriteLine("Creating a Person object and serializing it.")
        Dim p1 As New Person("Zighetti", "Barbara", 101)
        Dim writer As New FileStream(fileName, FileMode.Create)
        Dim ser As New DataContractSerializer(GetType(Person))
        ser.WriteObject(writer, p1)
        writer.Close()
    End Sub 

    Public Shared Sub ReadObject(ByVal fileName As String) 
        Console.WriteLine("Deserializing an instance of the object.")
        Dim fs As New FileStream(fileName, FileMode.Open)
        Dim reader As XmlDictionaryReader = _
            XmlDictionaryReader.CreateTextReader(fs, New XmlDictionaryReaderQuotas())
        Dim ser As New DataContractSerializer(GetType(Person))
        
        ' Deserialize the data and read it from the instance.
        Dim deserializedPerson As Person = CType(ser.ReadObject(reader, True), Person)
        reader.Close()
        fs.Close()
        Console.WriteLine(String.Format("{0} {1}, ID: {2}", deserializedPerson.FirstName, deserializedPerson.LastName, deserializedPerson.ID))
    End Sub 
End Class 

注解

使用 DataContractSerializer 类可以将类型实例序列化和反序列化为 XML 流或文档。Use the DataContractSerializer class to serialize and deserialize instances of a type into an XML stream or document. 例如,可以使用包含重要数据(如名称和地址)的属性创建一个名为 Person 的类型。For example, you can create a type named Person with properties that contain essential data, such as a name and address. 然后,可以创建和操作一个 Person 类实例,并在 XML 文档中写入所有其属性值以便于以后检索,或者在 XML 流中写入这些值以立即进行传输。You can then create and manipulate an instance of the Person class and write all of its property values in an XML document for later retrieval, or in an XML stream for immediate transport. 最重要的是,DataContractSerializer 用于序列化和反序列化在 Windows Communication Foundation (WCF)消息中发送的数据。Most important, the DataContractSerializer is used to serialize and deserialize data sent in Windows Communication Foundation (WCF) messages. 通过将 DataContractAttribute 属性 (Attribute) 应用于类,而将 DataMemberAttribute 属性 (Attribute) 应用于类成员,可以指定要序列化的属性 (Property) 和字段。Apply the DataContractAttribute attribute to classes, and the DataMemberAttribute attribute to class members to specify properties and fields that are serialized.

有关可序列化的类型的列表,请参阅数据协定序列化程序支持的类型For a list of types that can be serialized, see Types Supported by the Data Contract Serializer.

要使用 DataContractSerializer,请先创建一个类实例和适于写入或读取格式的对象;例如 XmlDictionaryWriter 实例。To use the DataContractSerializer, first create an instance of a class and an object appropriate to writing or reading the format; for example, an instance of the XmlDictionaryWriter. 然后调用 WriteObject 方法以保留数据。Then call the WriteObject method to persist the data. 要检索数据,请创建一个适于读取数据格式的对象(如 XML 文档的 XmlDictionaryReader),然后调用 ReadObject 方法。To retrieve data, create an object appropriate to reading the data format (such as an XmlDictionaryReader for an XML document) and call the ReadObject method.

有关使用 DataContractSerializer的详细信息,请参阅序列化和反序列化。For more information about using the DataContractSerializer, see Serialization and Deserialization.

您可以在客户端应用程序配置文件中使用<dataContractSerializer>元素设置数据协定序列化程序的类型。You can set the type of a data contract serializer using the <dataContractSerializer> element in a client application configuration file.

准备序列化或反序列化类Preparing Classes for Serialization or Deserialization

DataContractSerializerDataContractAttributeDataMemberAttribute 类结合使用。The DataContractSerializer is used in combination with the DataContractAttribute and DataMemberAttribute classes. 要准备序列化某个类,请将 DataContractAttribute 应用于该类。To prepare a class for serialization, apply the DataContractAttribute to the class. 对于返回要序列化的数据的类的每个成员,请应用 DataMemberAttributeFor each member of the class that returns data that you want to serialize, apply the DataMemberAttribute. 您可以序列化字段和属性,而无论其可访问性级别是什么:private、protected、internal、protected internal 或 public。You can serialize fields and properties, regardless of accessibility: private, protected, internal, protected internal, or public.

例如,您的架构指定具有 Customer 属性的 ID,但现有应用程序已使用一个名为 Person 且具有 Name 属性的类型。For example, your schema specifies a Customer with an ID property, but you already have an existing application that uses a type named Person with a Name property. 要创建一个符合协定的类型,请先将 DataContractAttribute 应用于该类。To create a type that conforms to the contract, first apply the DataContractAttribute to the class. 然后,将 DataMemberAttribute 应用于每个要序列化的字段或属性。Then apply the DataMemberAttribute to every field or property that you want to serialize.

备注

可以将 DataMemberAttribute 同时应用于私有和公共成员。You can apply the DataMemberAttribute to both private and public members.

XML 的最终格式不需要为文本。The final format of the XML need not be text. 相反,DataContractSerializer 以 XML infoset 形式写入数据,这样,即可以 XmlReaderXmlWriter 可识别的任意格式写入数据。Instead, the DataContractSerializer writes the data as an XML infoset, which allows you to write the data to any format recognized by the XmlReader and XmlWriter. 建议您使用 XmlDictionaryReaderXmlDictionaryWriter 类读取和写入数据,因为这两个类都经过优化可与 DataContractSerializer 一起使用。It is recommended that you use the XmlDictionaryReader and XmlDictionaryWriter classes to read and write, because both are optimized to work with the DataContractSerializer.

如果要创建的类具有在序列化或反序列化之前必须填充的字段或属性,请使用回叫特性,如版本容错序列化回调中所述。If you are creating a class that has fields or properties that must be populated before the serialization or deserialization occurs, use callback attributes, as described in Version-Tolerant Serialization Callbacks.

添加到已知类型的集合中Adding to the Collection of Known Types

在序列化或反序列化对象时,DataContractSerializer 必须“已知”该类型。When serializing or deserializing an object, it is required that the type is "known" to the DataContractSerializer. 首先,创建一个实现 IEnumerable<T>(如 List<T>)的类实例,并将已知类型添加到集合中。Begin by creating an instance of a class that implements IEnumerable<T> (such as List<T>) and adding the known types to the collection. 然后,使用接受 DataContractSerializer(例如,IEnumerable<T>)的重载之一创建 DataContractSerializer(Type, IEnumerable<Type>) 的实例。Then create an instance of the DataContractSerializer using one of the overloads that takes the IEnumerable<T> (for example, DataContractSerializer(Type, IEnumerable<Type>).

备注

与其他基元类型不同的是,默认情况下,DateTimeOffset 结构不是已知类型,因此必须将其手动添加到已知类型的列表中(请参阅数据协定已知类型)。Unlike other primitive types, the DateTimeOffset structure is not a known type by default, so it must be manually added to the list of known types (see Data Contract Known Types).

向前兼容性Forward Compatibility

DataContractSerializer 理解旨在与将来版本保持兼容的数据协定。The DataContractSerializer understands data contracts that have been designed to be compatible with future versions of the contract. 这些类型实现了 IExtensibleDataObject 接口。Such types implement the IExtensibleDataObject interface. 此接口具有 ExtensionData 属性,该属性返回 ExtensionDataObject 对象。The interface features the ExtensionData property that returns an ExtensionDataObject object. 有关详细信息,请参阅向前兼容的数据协定For more information, see Forward-Compatible Data Contracts.

在部分信任模式下运行Running under Partial Trust

在反序列化期间实例化目标对象时,DataContractSerializer 不调用目标对象的构造函数。When instantiating the target object during deserialization, the DataContractSerializer does not call the constructor of the target object. 如果你创作可从部分信任访问的 [DataContract] 类型(即,它是公共的,并且在应用了 AllowPartiallyTrustedCallers 特性的程序集中),并且执行一些与安全相关的操作,则必须注意不会调用构造函数。If you author a [DataContract] type that is accessible from partial trust (that is, it is public and in an assembly that has the AllowPartiallyTrustedCallers attribute applied) and that performs some security-related actions, you must be aware that the constructor is not called. 特别是,下面的方法无效:In particular, the following techniques do not work:

  • 如果尝试通过将构造函数指定为内部或私有或者将 LinkDemand 添加到构造函数中来限制部分信任访问;在部分信任模式下进行反序列化期间,这两种方法都没有任何效果。If you try to restrict partial trust access by making the constructor internal or private, or by adding a LinkDemand to the constructor -- neither of these have any effect during deserialization under partial trust.

  • 如果编码的类假定构造函数已运行,该类可能会进入一种可被利用的无效内部状态。If you code the class that assumes the constructor has run, the class may get into an invalid internal state that is exploitable.

构造函数

DataContractSerializer(Type)

初始化 DataContractSerializer 类的新实例,以便序列化或反序列化指定类型的对象。Initializes a new instance of the DataContractSerializer class to serialize or deserialize an object of the specified type.

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, 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 构造来保留图中的对象引用数据、自定义序列化的代理项以及在运行时映射 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.

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, 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, 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, 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.

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, 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, 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, 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.

属性

DataContractResolver

获取用于动态将 xsi:type 声明映射到已知协定类型的组件。Gets the component used to dynamically map xsi:type declarations to known contract types.

DataContractSurrogate

获取一个可扩展序列化或反序列化过程的代理项类型。Gets a surrogate type that can extend the serialization or deserialization process.

IgnoreExtensionDataObject

获取一个值,指定在对类进行序列化或反序列化时是否忽略类扩展提供的数据。Gets a value that specifies whether to ignore data supplied by an extension of the class when the class is being serialized or deserialized.

KnownTypes

获取一个类型集合,这些类型可能出现在使用此 DataContractSerializer 实例序列化的对象图中。Gets a collection of types that may be present in the object graph serialized using this instance of the DataContractSerializer.

MaxItemsInObjectGraph

获取对象图中要序列化或反序列化的最大项数。Gets the maximum number of items in an object graph to serialize or deserialize.

PreserveObjectReferences

获取一个值,指定是否使用非标准的 XML 结构来保留对象引用数据。Gets a value that specifies whether to use non-standard XML constructs to preserve object reference data.

SerializeReadOnlyTypes

获取指定只读类型是否序列化的值。Gets a value that specifies whether read-only types are serialized.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsStartObject(XmlDictionaryReader)

确定是否将 XmlDictionaryReader 定位在可反序列化的对象上。Determines whether the XmlDictionaryReader is positioned on an object that can be deserialized.

IsStartObject(XmlReader)

确定是否将 XmlReader 定位在可反序列化的对象上。Determines whether the XmlReader is positioned on an object that can be deserialized.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ReadObject(Stream)

使用 Stream 读取 XML 流或文档,然后返回反序列化的对象。Reads the XML stream or document with a Stream and returns the deserialized object.

(继承自 XmlObjectSerializer)
ReadObject(XmlDictionaryReader)

使用 XmlDictionaryReader 读取 XML 文档或流,然后返回反序列化的对象。Reads the XML document or stream with an XmlDictionaryReader and returns the deserialized object.

(继承自 XmlObjectSerializer)
ReadObject(XmlDictionaryReader, Boolean)

使用 XmlDictionaryReader 读取 XML 流并返回反序列化的对象,并且还指定在读取其值之前是否进行检查以验证对象名称。Reads the XML stream with an XmlDictionaryReader and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value.

ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)

读取一个 XML 文档或文档流,并返回反序列化的对象。Reads an XML document or document stream and returns the deserialized object. 此方法包括用于指定是否验证对象名称的参数和用于在运行时映射 xsi:type 声明的解析器。The method includes a parameter to specify whether the object name is verified is validated, and a resolver for mapping xsi:type declarations at runtime.

ReadObject(XmlReader)

使用 XmlReader 读取 XML 流,并返回反序列化的对象。Reads the XML stream with an XmlReader and returns the deserialized object.

ReadObject(XmlReader, Boolean)

使用 XmlReader 读取 XML 流并返回反序列化的对象,并且还指定在读取其值之前是否进行检查以验证对象名称。Reads the XML stream with an XmlReader and returns the deserialized object, and also specifies whether a check is made to verify the object name before reading its value.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
WriteEndObject(XmlDictionaryWriter)

使用 XmlDictionaryWriter 写入结束 XML 元素。Writes the closing XML element using an XmlDictionaryWriter.

WriteEndObject(XmlWriter)

使用 XmlWriter 写入结束 XML 元素。Writes the closing XML element using an XmlWriter.

WriteObject(Stream, Object)

使用指定的 Stream 将对象的完整内容(开始、内容和结束)写入 XML 文档或流。Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified Stream.

(继承自 XmlObjectSerializer)
WriteObject(XmlDictionaryWriter, Object)

使用指定的 XmlDictionaryWriter 将对象的完整内容(开始、内容和结束)写入 XML 文档或流。Writes the complete content (start, content, and end) of the object to the XML document or stream with the specified XmlDictionaryWriter.

(继承自 XmlObjectSerializer)
WriteObject(XmlDictionaryWriter, Object, DataContractResolver)

使用指定的 XmlDictionaryWriter 将所有对象数据(开始 XML 元素、内容和结束元素)写入 XML 文档或流。Writes all the object data (starting XML element, content, and enclosing element) to an XML document or stream using the specified XmlDictionaryWriter. 此方法包含用于在运行时映射 xsi:type 声明的解析器。The method includes a resolver for mapping xsi:type declarations at runtime.

WriteObject(XmlWriter, Object)

使用 XmlWriter 将所有对象数据(开始 XML 元素、内容和结束元素)写入 XML 文档或流。Writes all the object data (starting XML element, content, and closing element) to an XML document or stream with an XmlWriter.

WriteObjectContent(XmlDictionaryWriter, Object)

使用 XmlDictionaryWriter 写入 XML 内容。Writes the XML content using an XmlDictionaryWriter.

WriteObjectContent(XmlWriter, Object)

使用 XmlWriter 写入 XML 内容。Writes the XML content using an XmlWriter.

WriteStartObject(XmlDictionaryWriter, Object)

使用 XmlDictionaryWriter 写入开始 XML 元素。Writes the opening XML element using an XmlDictionaryWriter.

WriteStartObject(XmlWriter, Object)

使用 XmlWriter 写入开始 XML 元素。Writes the opening XML element using an XmlWriter.

扩展方法

GetSerializationSurrogateProvider(DataContractSerializer)

返回用于此序列化程序的代理项序列化提供程序。Returns the surrogate serialization provider for this serializer.

SetSerializationSurrogateProvider(DataContractSerializer, ISerializationSurrogateProvider)

指定用于此 DataContractSerializer 的代理项序列化提供程序。Specifies a surrogate serialization provider for this DataContractSerializer.

适用于

线程安全性

此类的实例是线程安全的,除非实例与 IDataContractSurrogateDataContractResolver的实现一起使用。Instances of this class are thread safe except when the instance is used with an implementation of the IDataContractSurrogate or DataContractResolver.

另请参阅