2
Answers

Reading a serialized file.

r

r

16y
2.5k
1

Basically, I have 5 textboxs where I enter information to be serialized to a text file. I wan't to display all the contents from the text file into a multiline textbox, this is where my problem occurs.

I can only read the first 'record' of the text file, nothing else gets displayed in the textbox. The only way im able to get everything is by creating an endless loop and catching a serialization exception when it gets to the end, which breaks the loop.

Basically, What is the proper way of doing this?

BinaryFormatter read = new BinaryFormatter();

FileStream file;

file = new FileStream(openFile.FileName, FileMode.Open, FileAccess.Read);

try

{

for (int i = 0; i < 2; i++)

{

Student student = (Student)read.Deserialize(file);

txtDisplay.Text += " " + student.LastName.ToString() + ",";

txtDisplay.Text += " " + student.FirstName.ToString() + ": ";

txtDisplay.Text += " " + student.ID.ToString();

txtDisplay.Text += " " + student.Class.ToString();

txtDisplay.Text += " " + student.Grade.ToString() + "\r\n";

i--;

}

}

catch (SerializationException)

{

}

 

This can't be the best way...

Answers (2)