Thursday, June 5, 2008

How to serialize c# object to binary data.

       The simplest way to serialize object, that is use class BinaryFormatter.

Sample of serialization to file:

using (FileStream stream = new FileStream(tempFilePath, FileMode.Create))
BinaryFormatter b = new BinaryFormatter();
b.Serialize(stream, originalData);

Sample of deserialization to file:

Data actualData;
using (FileStream stream = new FileStream(tempFilePath, FileMode.Open))
BinaryFormatter b = new BinaryFormatter();
actualData = (Data)b.Deserialize(stream);

where class Data has attribute [Serializable].

See sources on “C# tips samples” project.


BinaryFormatter Class

The simplest way to store object to .xml file in c#.

1 comment:

Unknown said...

Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.