共用方式為


HOW TO:還原序列化物件

當您還原序列化物件時,傳輸格式決定您會建立資料流或檔案物件。決定傳輸格式後,您可視需要呼叫 SerializeDeserialize 方法。

還原序列化物件

  1. 使用要還原序列化的物件型別,建構 XmlSerializer

  2. 呼叫 Deserialize 方法以產生物件的複本。在還原序列化時,您必須將傳回之物件轉換為原始的型別,如下面的範例所示,將物件還原序列化為檔案 (不過它也能還原序列化成資料流)。

    Dim myObject As MySerializableClass
    ' Construct an instance of the XmlSerializer with the type
    ' of object that is being deserialized.
    Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(MySerializableClass))
    ' To read the file, create a FileStream.
    Dim myFileStream As FileStream = _
    New FileStream("myFileName.xml", FileMode.Open)
    ' Call the Deserialize method and cast to the object type.
    myObject = CType( _
    mySerializer.Deserialize(myFileStream), MySerializableClass)
    
    MySerializableClass myObject;
    // Construct an instance of the XmlSerializer with the type
    // of object that is being deserialized.
    XmlSerializer mySerializer = 
    new XmlSerializer(typeof(MySerializableClass));
    // To read the file, create a FileStream.
    FileStream myFileStream = 
    new FileStream("myFileName.xml", FileMode.Open);
    // Call the Deserialize method and cast to the object type.
    myObject = (MySerializableClass) 
    mySerializer.Deserialize(myFileStream)
    

另請參閱

工作

HOW TO:序列化物件

概念

XML 序列化簡介

建置日期:2010-03-10