Multiple Object Type Serialization
-
Hi All, I was wondering if someone could point me in the right direction. I am trying to figure out how to hold multiple object types in a single serialized file. E.g. If i was wanting to save settings class and a person class in a single serialized file, what would be the best method... also, how can you determine and objects type when deserializing the file. Thanks in advance, TF
-
Hi All, I was wondering if someone could point me in the right direction. I am trying to figure out how to hold multiple object types in a single serialized file. E.g. If i was wanting to save settings class and a person class in a single serialized file, what would be the best method... also, how can you determine and objects type when deserializing the file. Thanks in advance, TF
string ConfigFile = @"c:\MyConfig.cfg";
// Save
int a = 10;string b = "hi";
DataTable c = new DataTable();
Stream StreamFile = File.Open(ConfigFile, FileMode.CreateNew);
BinaryFormatter binformat = new BinaryFormatter();
binformat.Serialize(StreamFile, a);
binformat.Serialize(StreamFile, b);
binformat.Serialize(StreamFile, c);
StreamFile.Close();// Read
Stream StreamFile = File.Open(ConfigFile, FileMode.Open);
BinaryFormatter binformat = new BinaryFormatter();int a1 = (int)binformat.Deserialize(StreamFile);
string b1 = (string)binformat.Deserialize(StreamFile);
DataTable c1 = (DataTable)binformat.Deserialize(StreamFile);
StreamFile.Close();I know nothing , I know nothing ...
-
string ConfigFile = @"c:\MyConfig.cfg";
// Save
int a = 10;string b = "hi";
DataTable c = new DataTable();
Stream StreamFile = File.Open(ConfigFile, FileMode.CreateNew);
BinaryFormatter binformat = new BinaryFormatter();
binformat.Serialize(StreamFile, a);
binformat.Serialize(StreamFile, b);
binformat.Serialize(StreamFile, c);
StreamFile.Close();// Read
Stream StreamFile = File.Open(ConfigFile, FileMode.Open);
BinaryFormatter binformat = new BinaryFormatter();int a1 = (int)binformat.Deserialize(StreamFile);
string b1 = (string)binformat.Deserialize(StreamFile);
DataTable c1 = (DataTable)binformat.Deserialize(StreamFile);
StreamFile.Close();I know nothing , I know nothing ...
Thanks :) Just another quick question which comes from the example; If you had; string ConfigFile = @"c:\MyConfig.cfg"; // Save int a = 1; int b = 2; Stream StreamFile = File.Open(ConfigFile, FileMode.CreateNew); BinaryFormatter binformat = new BinaryFormatter(); binformat.Serialize(StreamFile, a); binformat.Serialize(StreamFile, b); StreamFile.Close(); ... to create the file, when reading back from the file, how can you indentify which int you would be casting back? Cheers TF
-
Thanks :) Just another quick question which comes from the example; If you had; string ConfigFile = @"c:\MyConfig.cfg"; // Save int a = 1; int b = 2; Stream StreamFile = File.Open(ConfigFile, FileMode.CreateNew); BinaryFormatter binformat = new BinaryFormatter(); binformat.Serialize(StreamFile, a); binformat.Serialize(StreamFile, b); StreamFile.Close(); ... to create the file, when reading back from the file, how can you indentify which int you would be casting back? Cheers TF
Hi , you are welcome for your Question : if you Save your variables like this order ... int a = 1; int b = 9; int c = 3; then you can read JUST like this a = (int)Deserlaize ( .... ) b = (int)Deserlaize ( .... ) c = (int)Deserlaize ( .... ) in the same order , and the output will be a = 1 , b = 9 , c = 3 in other word you can't read (int b) , with out reading ( int a ) .... P.S : I am sorry , I can't found the right word to present this but may be ( Book1 Above Book2 Above Book3 ) .... you can't get book2 , with out (moving or push away ) book1 .... I am really sorry for this bad english .... but if you got the idea , please let me know , I will be happy :rose:
I know nothing , I know nothing ...