0

I need to save an object, its serializable but I donot want to use XML. Is it possible to write the raw bytes of the object and then read it off the disk to create the object again?

Thanks for the help!

2 Answers 2

2

Use a BinaryFormatter:

var formatter = new BinaryFormatter();

// Serialize
using (var stream = File.OpenWrite(path))
{
    formatter.Serialize(stream, yourObject);
}

...

// Deserialize
using (var stream = File.OpenRead(path))
{
    YourType yourObject = (YourType)formatter.Deserialize(stream);
}
Sign up to request clarification or add additional context in comments.

1 Comment

but for passing objects between projects, both of them need to reside in a common assembly/namespace
1

Yes, it is called binary serialization. There are some good examples on the MSDN site:

http://msdn.microsoft.com/en-us/library/4abbf6k0(v=vs.100).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.