方法 : オブジェクトを逆シリアル化する

オブジェクトを逆シリアル化する場合、転送形式によって、ストリーム オブジェクトとファイル オブジェクトのどちらを作成するかが決定されます。転送形式を決定したら、必要に応じて Serialize メソッドまたは Deserialize メソッドを呼び出すことができます。

オブジェクトを逆シリアル化するには

  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)
    

参照

処理手順

方法 : オブジェクトをシリアル化する

概念

XML シリアル化の概要

ビルド日:2010-03-10