DataContractSerializer.ReadObject 方法

定义

读取 XML 流,并返回反序列化的对象。

重载

ReadObject(XmlReader)

使用 XmlReader 读取 XML 流,并返回反序列化的对象。

ReadObject(XmlDictionaryReader, Boolean)

使用 XmlDictionaryReader 读取 XML 流并返回反序列化的对象,并且还指定在读取其值之前是否进行检查以验证对象名称。

ReadObject(XmlReader, Boolean)

使用 XmlReader 读取 XML 流并返回反序列化的对象,并且还指定在读取其值之前是否进行检查以验证对象名称。

ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)

读取一个 XML 文档或文档流,并返回反序列化的对象。 此方法包括用于指定是否验证对象名称的参数和用于在运行时映射 xsi:type 声明的解析器。

ReadObject(XmlReader)

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

使用 XmlReader 读取 XML 流,并返回反序列化的对象。

public:
 override System::Object ^ ReadObject(System::Xml::XmlReader ^ reader);
public override object ReadObject (System.Xml.XmlReader reader);
public override object? ReadObject (System.Xml.XmlReader reader);
override this.ReadObject : System.Xml.XmlReader -> obj
Public Overrides Function ReadObject (reader As XmlReader) As Object

参数

reader
XmlReader

用于读取 XML 流的 XmlReader

返回

已反序列化的对象。

适用于

ReadObject(XmlDictionaryReader, Boolean)

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

使用 XmlDictionaryReader 读取 XML 流并返回反序列化的对象,并且还指定在读取其值之前是否进行检查以验证对象名称。

public:
 override System::Object ^ ReadObject(System::Xml::XmlDictionaryReader ^ reader, bool verifyObjectName);
public override object ReadObject (System.Xml.XmlDictionaryReader reader, bool verifyObjectName);
public override object? ReadObject (System.Xml.XmlDictionaryReader reader, bool verifyObjectName);
override this.ReadObject : System.Xml.XmlDictionaryReader * bool -> obj
Public Overrides Function ReadObject (reader As XmlDictionaryReader, verifyObjectName As Boolean) As Object

参数

reader
XmlDictionaryReader

用于读取 XML 流的 XmlDictionaryReader

verifyObjectName
Boolean

要检查对象名称是否与构造函数中提供的根名称值相对应,则为 true;否则为 false

返回

已反序列化的对象。

例外

如果将 verifyObjectName 参数设置为 true,则元素名称和命名空间与构造函数中设置的值不对应。

示例

下面的示例读取 XML 文档并反序列化一个对象实例。

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

适用于

ReadObject(XmlReader, Boolean)

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

使用 XmlReader 读取 XML 流并返回反序列化的对象,并且还指定在读取其值之前是否进行检查以验证对象名称。

public:
 override System::Object ^ ReadObject(System::Xml::XmlReader ^ reader, bool verifyObjectName);
public override object ReadObject (System.Xml.XmlReader reader, bool verifyObjectName);
public override object? ReadObject (System.Xml.XmlReader reader, bool verifyObjectName);
override this.ReadObject : System.Xml.XmlReader * bool -> obj
Public Overrides Function ReadObject (reader As XmlReader, verifyObjectName As Boolean) As Object

参数

reader
XmlReader

用于读取 XML 流的 XmlReader

verifyObjectName
Boolean

要检查对象名称是否与构造函数中提供的根名称值相对应,则为 true;否则为 false

返回

已反序列化的对象。

例外

如果将 verifyObjectName 参数设置为 true,则元素名称和命名空间与构造函数中设置的值不对应。

适用于

ReadObject(XmlDictionaryReader, Boolean, DataContractResolver)

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

读取一个 XML 文档或文档流,并返回反序列化的对象。 此方法包括用于指定是否验证对象名称的参数和用于在运行时映射 xsi:type 声明的解析器。

public:
 System::Object ^ ReadObject(System::Xml::XmlDictionaryReader ^ reader, bool verifyObjectName, System::Runtime::Serialization::DataContractResolver ^ dataContractResolver);
public object? ReadObject (System.Xml.XmlDictionaryReader reader, bool verifyObjectName, System.Runtime.Serialization.DataContractResolver? dataContractResolver);
public object ReadObject (System.Xml.XmlDictionaryReader reader, bool verifyObjectName, System.Runtime.Serialization.DataContractResolver dataContractResolver);
override this.ReadObject : System.Xml.XmlDictionaryReader * bool * System.Runtime.Serialization.DataContractResolver -> obj
Public Function ReadObject (reader As XmlDictionaryReader, verifyObjectName As Boolean, dataContractResolver As DataContractResolver) As Object

参数

reader
XmlDictionaryReader

用于读取内容的 XML 阅读器。

verifyObjectName
Boolean

若要验证对象名称,则为 true;否则为 false

dataContractResolver
DataContractResolver

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

返回

已反序列化的对象。

适用于