deserialization problem
-
Hi there, if i got an 3 arraylist to be serialize, each with different type(Point[] or int) how do i deserialize them back to the 3 different arraylist accordingly. Should i save the number of items in each of the arraylist? Check the type? What other methods would u suggest? Thanks in advance.
-
Hi there, if i got an 3 arraylist to be serialize, each with different type(Point[] or int) how do i deserialize them back to the 3 different arraylist accordingly. Should i save the number of items in each of the arraylist? Check the type? What other methods would u suggest? Thanks in advance.
If you serialize the lists into separate streams, just remember which list resides in which stream and you'll be fine. If you serialize the lists into one common stream, though, make sure you deserialize them in the exact same order they were serialized.
-
If you serialize the lists into separate streams, just remember which list resides in which stream and you'll be fine. If you serialize the lists into one common stream, though, make sure you deserialize them in the exact same order they were serialized.
i need to save all the arraylists into a common arraylist and then serialize it because i want to save it into a file. Is this correct? Below is part of the code i wrote: In Save()
ArrayList arr = new ArrayList(); arr.Add(Arraylist1);//arraylist containing Point[] for(int i=0;i { GraphicsPathData gpd = new GraphicsPathData((GraphicsPath)PathList[i]); Stream gpdStream = GraphicsPathData.Serialize(gpd); arr.Add(gpdStream); } arr.Add(Arraylist1);//arraylist containing enum formatter.Serialize(myStream, arr); myStream.Close();
in OpenFile()ArrayList arr =new ArrayList(); if (openFileDialog.ShowDialog() == DialogResult.OK) { filename = openFileDialog.FileName; Stream myStream = openFileDialog.OpenFile(); if (myStream != null) { IFormatter formatter = new BinaryFormatter(); arr = (ArrayList) formatter.Deserialize(myStream); myStream.Close(); .................
However the problem here, how can i separate the arr into the correct arraylist? Thanks for your help -
i need to save all the arraylists into a common arraylist and then serialize it because i want to save it into a file. Is this correct? Below is part of the code i wrote: In Save()
ArrayList arr = new ArrayList(); arr.Add(Arraylist1);//arraylist containing Point[] for(int i=0;i { GraphicsPathData gpd = new GraphicsPathData((GraphicsPath)PathList[i]); Stream gpdStream = GraphicsPathData.Serialize(gpd); arr.Add(gpdStream); } arr.Add(Arraylist1);//arraylist containing enum formatter.Serialize(myStream, arr); myStream.Close();
in OpenFile()ArrayList arr =new ArrayList(); if (openFileDialog.ShowDialog() == DialogResult.OK) { filename = openFileDialog.FileName; Stream myStream = openFileDialog.OpenFile(); if (myStream != null) { IFormatter formatter = new BinaryFormatter(); arr = (ArrayList) formatter.Deserialize(myStream); myStream.Close(); .................
However the problem here, how can i separate the arr into the correct arraylist? Thanks for your helpYou don't need a common arraylist to serialize several arraylists into one file. Just make several calls to
formatter.Serialize
passing the same stream:formatter.Serialize(myStream, arr1)
formatter.Serialize(myStream, arr2)
formatter.Serialize(myStream, arr3)To restore the arraylists, make the calls to
formatter.Deserialize
in the exact same order:arr1 = (ArrayList) formatter.Deserialize(myStream);
arr2 = (ArrayList) formatter.Deserialize(myStream);
arr3 = (ArrayList) formatter.Deserialize(myStream); -
You don't need a common arraylist to serialize several arraylists into one file. Just make several calls to
formatter.Serialize
passing the same stream:formatter.Serialize(myStream, arr1)
formatter.Serialize(myStream, arr2)
formatter.Serialize(myStream, arr3)To restore the arraylists, make the calls to
formatter.Deserialize
in the exact same order:arr1 = (ArrayList) formatter.Deserialize(myStream);
arr2 = (ArrayList) formatter.Deserialize(myStream);
arr3 = (ArrayList) formatter.Deserialize(myStream);