Serialization Question

  • Thread starter Thread starter Silesian
  • Start date Start date
S

Silesian

I realize that this is a newbie question but maybe someone will be able to
tell me what am I doing wrong.
createFile() gets called each time I have the user generate some numbers
which then get added to the binlist ArrayList.
I'm able to serialize and deserialize the Arraylist. However, when I
deserialize the ArrayList, it will only show the first set of ints. I
looked at the bin file and it does get appended to each time. Any help will
be appreciated.
Rich

private void createFile()
{
foreach (int a in num)
{
binlist.Add (a);
}

Stream streamAppend = File.Open("Numbers.bin",FileMode.Append);
bf.Serialize (streamAppend,binlist);
streamAppend.Close ();
Stream streamRead = File.OpenRead("Numbers.bin");
binlist=(ArrayList)bf.Deserialize (streamRead);
streamRead.Close ();
MessageBox.Show (binlist.Count .ToString ());
for(int i = 0; i < binlist.Count ; i ++)
{
MessageBox.Show (binlist.ToString ());
}
 
Serialization does not support appending (at least not that I have read
anywhere, MSDN does not explicitly say this, but given how serialization
works, I can't imagine that it does work).

If you need to modify a serialized component, then you should deserialize it
first, then make your changes and then serialize it back, overwriting the
original file.

--
HTH

Kyril Magnos

Question of the day:
What is Mono?
A) Disease where the lymph nodes become swollen.
B) A single sound
C) A synonym for one
D) A port of .NET meant to royally irritate MSFT
E) All of the above.

|I realize that this is a newbie question but maybe someone will be able to
| tell me what am I doing wrong.
| createFile() gets called each time I have the user generate some numbers
| which then get added to the binlist ArrayList.
| I'm able to serialize and deserialize the Arraylist. However, when I
| deserialize the ArrayList, it will only show the first set of ints. I
| looked at the bin file and it does get appended to each time. Any help
will
| be appreciated.
| Rich
|
| private void createFile()
| {
| foreach (int a in num)
| {
| binlist.Add (a);
| }
|
| Stream streamAppend = File.Open("Numbers.bin",FileMode.Append);
| bf.Serialize (streamAppend,binlist);
| streamAppend.Close ();
| Stream streamRead = File.OpenRead("Numbers.bin");
| binlist=(ArrayList)bf.Deserialize (streamRead);
| streamRead.Close ();
| MessageBox.Show (binlist.Count .ToString ());
| for(int i = 0; i < binlist.Count ; i ++)
| {
| MessageBox.Show (binlist.ToString ());
| }
|
|
 
Back
Top