共用方式為


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)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
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)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
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)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
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)

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

DataContractResolver 的實作,用來將 xsi:type 宣告對應至資料合約型別。

傳回

已還原序列化的物件。

適用於