I'm having problems when I try to deserialize a data file. The code is based on the tutorial, 'serialize objects to a file', by The Reddest.
When I restart my application it seems to fail to repopulate the cars list. I have used similar techniques in Delphi but I have always had to put code in place to take the deserialized list-item data and mechanically add each item back on to the list. The serialisation mechanism in .NET seems to be more automated.
I'm not sure if the 'post comments' thread on the original tutorial has died so I thought I'd raise the question on this Forum.
To demonstrate the problem, I have posted the complete source code of a simple c# console application to the original tutorial's thread. I suspect I've been careless and missed something out in the code, but I'm lost and out of ideas.
Any help would be much appreciated.
06/08/2009 - 08:44
After a quick glance at the code I didn't immediately see anything wrong. When you serialize the collection, is the file data3.txt being created?
06/08/2009 - 15:53
Yes it is, and the content seems reasonable. The car and owner strings are clear to see even with Notepad.
When it runs I get,
enter command >i
initialised
Entries =2
0 Saturn0,Sky0,2007
1 Nissan0,Maxima0,2006
enter command >i
initialised
Entries =4
0 Saturn0,Sky0,2007
1 Nissan0,Maxima0,2006
2 Saturn1,Sky1,2008
3 Nissan1,Maxima1,2007
okay, let save the data.
enter command >s
saving
document serialising
TCAR serialising
TOWNER serialising
TCAR serialising
TOWNER serialising
TCAR serialising
TOWNER serialising
TCAR serialising
TOWNER serialising
Entries =4
0 Saturn0,Sky0,2007
1 Nissan0,Maxima0,2006
2 Saturn1,Sky1,2008
3 Nissan1,Maxima1,2007
enter command >q
quit
..this creates 4 car entries and then writes to data3.txt
When I run the application again and attempt to load data from data3.txt I get,
enter command >l
loading
document deserialising
TOWNER deserialising
TOWNER deserialising
TOWNER deserialising
TOWNER deserialising
TCAR deserialising
TCAR deserialising
TCAR deserialising
TCAR deserialising
Entries =0
enter command >q
quit
It's making all the right calls to the deserialise functions, but the end result is an empty list.
06/08/2009 - 17:24
Ok, I see the problem now. In DeSerializeObject you're creating and returning a TDocument, which is never used anywhere. The command interpretting code should probably look something like this:
{
Console.WriteLine("loading");
// The return value of DeSerializeObject holds the
// car objects.
document = filer.DeSerializeObject(fname);
}
06/09/2009 - 12:47
Well there she blows!
Thanks, that fixed the problem.
Oh well, it's the simple mistakes that are hardest to bare. If you could see my face, I guess you could call me 'the reddest' right now.
Do you think that the serialisation mechanism is a bit complicated? Not so much in the code needed to make it work but the content of the data files it produces?
Yesterday, in preparing to reply to your response, I tried several variants - 2 entries, 3 entries, 4 entries and so on. I spent some time using Debug.exe to view the file content. Within the data file, there are all sorts of entries associated with the application as well as the serialised data that I did not expect. Is there any way to force the mechanism to simplify its output? I cannot help thinking that if I had a data loss or a corruption problem at some point I would find it very hard to recover the data.
I hope you will be willing to discuss. But again, many thanks for your help and the tutorial.
06/09/2009 - 13:51
Binary serialization is often overkill for most applications. If you've got circular references or multiple instances of the same object, then you'd want to go this route.
Most of the time, however, XML serialization will get the job done. The output file is human readable the process is a lot easier. It just has some limitations that can be overcome by using binary.
We've got a tutorial on C# XML Serialization that you may want to glance at.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.