如何:反序列化对象
当您反序列化对象时,传输格式确定您将创建流还是文件对象。确定了传输格式之后,就可以根据需要调用 Serialize 或 Deserialize 方法。
反序列化对象
使用要反序列化的对象的类型构造 XmlSerializer。
调用 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)
请参见
任务
概念
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。