none
How to serialize and deserialize an object

    Question

  • Hi,

    I'd created a class named ShoppingCart, I only know that to make the instances of a class serializable, [Serializable] attribute should be exist in the class declaration. But I don't know what method should I call in order to serialize and deserialize an object. I wanna do that because this object needed to be saved in and retrieved from database. Anybody can tell me? Thanks!

    Friday, December 30, 2005 8:31 AM

Answers

  • As mentioned, if you do not need to control serialization you do not need to implement ISerializable as I mentioned. Here is an example, more or less from MSDN documentation. I created a Windows Application and added this code in the form. As you will see the oSerialize will be deserialized into oDeserialized and they will both contain the same values.

    private void Form1_Load(object sender, System.EventArgs e)
    {
       Serialized oSerialize =
    new Serialized();
       oSerialize.strText = "Test";
       oSerialize.nValue = 99;
       oSerialize.dtCreted = DateTime.Now;

       FileStream fs = new FileStream("serialized.dat", FileMode.Create);
       BinaryFormatter formatter =
    new BinaryFormatter();
       formatter.Serialize(fs, oSerialize);
       fs.Position = 0;
       Serialized oDeserialized = (Serialized)formatter.Deserialize(fs);
       fs.Close();
    }

     

    [Serializable]
    public class Serialized
    {
       public String strText;
       public Int32 nValue;
       public DateTime dtCreted;
    }

    Friday, December 30, 2005 9:41 AM

All replies

  • You should also implement the ISerializable interface if you want to control serialization of your class. The link has example code how it can be implemented
    Friday, December 30, 2005 8:54 AM
  • Thanks for your reply, Andreas. But can you explain in details?
    Friday, December 30, 2005 8:57 AM
  • you shouldn't have to implement ISerializable unless you want to override the way it serializes. In most cases, the Serializable attribute will be sufficient.

    use a binaryformatter

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeserializationformattersbinarybinaryformatterclasstopic.asp

    Friday, December 30, 2005 9:08 AM
  • Thanks for your reply. Can you show me a simple code sample of how to serialize and deserialize?
    Friday, December 30, 2005 9:23 AM
  • I dont know how to get more simple than the Serialize() and Deserialize() as outlined in that example link. . .

    Is there something you dont understand there?

    Friday, December 30, 2005 9:27 AM
  • As mentioned, if you do not need to control serialization you do not need to implement ISerializable as I mentioned. Here is an example, more or less from MSDN documentation. I created a Windows Application and added this code in the form. As you will see the oSerialize will be deserialized into oDeserialized and they will both contain the same values.

    private void Form1_Load(object sender, System.EventArgs e)
    {
       Serialized oSerialize =
    new Serialized();
       oSerialize.strText = "Test";
       oSerialize.nValue = 99;
       oSerialize.dtCreted = DateTime.Now;

       FileStream fs = new FileStream("serialized.dat", FileMode.Create);
       BinaryFormatter formatter =
    new BinaryFormatter();
       formatter.Serialize(fs, oSerialize);
       fs.Position = 0;
       Serialized oDeserialized = (Serialized)formatter.Deserialize(fs);
       fs.Close();
    }

     

    [Serializable]
    public class Serialized
    {
       public String strText;
       public Int32 nValue;
       public DateTime dtCreted;
    }

    Friday, December 30, 2005 9:41 AM