Hi everyone!
I'm developing a C++/CLR Windows Forms app in visual Studio 05.
I wanted the user to be able to save the current state of the form which I acheived by serializing the form with the code in my 'Save' function:
// I have created a class 'MyPersistantData' to store the forms
// state
// Serialize the form state to an XML file
MyPersistantData^ dat = gcnew MyPersistantData;
TextWriter^ writer = gcnew StreamWriter(saveFileDialog->FileName );
XmlSerializer ^ myXmlSerializer = gcnew XmlSerializer(dat->GetType() );
myXmlSerializer->Serialize( writer, dat );
writer->Close();
This worked great and saved all my form variables to an XML file.
However I am having major problems trying to de-serialize. This is the code in my 'Open' function I have so far:
MyPersistantData^ dat = gcnew MyPersistantData;
XmlSerializer^ myXmlDeSerializer =
gcnew XmlSerializer( dat->GetType() );
FileStream^ reader =
gcnew FileStream(openFileDialog->FileName, FileMode::Open );
XmlReader^ XReader = gcnew XmlTextReader( reader );
// These lines are fine.
// But the following line throws an exception during runtime:
dat = dynamic_cast< MyPersistantData^ >
(myXmlDeSerializer->Deserialize( reader ) );
Do I need to overload the assignment operator for this class or is there something else i need to do?
Cheers
Emma