Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Serialization
 How to: Deserialize an Object

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Developer's Guide 
How to: Deserialize an Object 

When you deserialize an object, the transport format determines whether you will create a stream or file object. After the transport format is determined, you can call the Serialize or Deserialize methods, as required.

To deserialize an object

  1. Construct a XmlSerializer using the type of the object to deserialize.

  2. Call the Deserialize method to produce a replica of the object. When deserializing, you must cast the returned object to the type of the original, as shown in the following example, which deserializes the object into a file (although it could also be deserialized into a stream).

    Visual Basic
    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)
    

    C#
    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)
    

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
which deserializes the object into a file ?      Yangxin   |   Edit   |   Show History

which deserializes the object into a file ? or

which deserializes the object from a file

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker