Link to home
Start Free TrialLog in
Avatar of jkrill
jkrill

asked on

Serialization alternatives/options.

I'm looking into creating an email client with C#. I want all the data stored in one file, but don't want to have to use a seperate data source (Access DB, SqlServer, etc).  I'm leary about using the standard serialization, because it would seem that everytime something is changed (new messages, message moved, etc), i would have to rewrite the file (i may be wrong on this).  In the event that say, new mail has been retrieved, and the program was closed abnormally (prior to a save), these new messages would be lost.  I liked the idea of the IStorage interface in C++, and was wondering if there was any equivalent in C#. I also looked into using XML, but this would seem to store everything in plain text.  Can anyone offer any insights into any possible storage options?

Thanks,
Joe Krill
Avatar of Pickle
Pickle

If you are looking to store emails, XML would be an ideal storage medium.

1. All email is plaintext, even attachements are UU or BinHex encoded to plaintext anyways.

2. All of the .NET data access components are backended by XML so you would still be able to use DataSets, DataTables, DataRows, etc...

I would hope that you don't expect your program to just die without any notice or chance to write to disk.  The only time I could see this happening is if they shut down the computer without closing your program.  But even MS Outlook will not let you shutdown your computer without first closing it.

If you just make sure that in the destructor of your main form or one of your main objects that you write the DataSet out to disk you should be just fine.  That destructor will still get called in the instance of almost any error or problem that your application may encounter.

Let me know if you have any, more-specific questions.
Avatar of jkrill

ASKER

XML would be perfect, except for the fact that I don't want the data stored in plain text on the disk.  Is XML capable of this?
Just do this to encrypt the xml as you write it out to disk:
     //Create the file stream to handle the output file.
     FileStream fout = new FileStream(@"C:\mailstore.mls", FileMode.OpenOrCreate, FileAccess.Write);

     RijndaelManaged rm = new RijndaelManaged();
     
     byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
     byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};

     //Create a CryptoStream, pass it the NetworkStream, and encrypt it with the Rijndael class.
     CryptoStream encStream = new CryptoStream(NetStream, rm.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
 
     myDataSet.WriteXml(encStream);
 
     encStream.Close();  
     fout.Close();

You can just do this backwards to pull it back into a DataSet...

If you want more info on the Key and IV look up RijndaelManaged in the Visual Studio help.
ASKER CERTIFIED SOLUTION
Avatar of Pickle
Pickle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkrill

ASKER

Perfect - exactly what I was looking for.  Thank You!