Binaryformatter, unreadable junk

2

2G

Hi,

When I serialize a object using the binaryformatter and save it to a file,
the file contains some parts of unreadable junk.
Should I do some encoding on the bytearray or something before saving it ?
All works fine when I use the soapformatter.

public byte[] Serialize(object o, SerializationFormat format) {

IFormatter iFor = null;

switch(format) {

case SerializationFormat.BINARY:

iFor = new BinaryFormatter();

break;

case SerializationFormat.SOAP:

iFor = new SoapFormatter();

break;

}

Stream mem = new MemoryStream();

iFor.Serialize(mem, o);

mem.Position = 0;

byte[] buffer = new byte[mem.Length];

mem.Read(buffer, 0, buffer.Length);

mem.Close();

return buffer;

}

----------
byte[] b = sl.Serialize(sett, SerializationFormat.BINARY);

//byte[] b = sl.Serialize(sett, SerializationFormat.SOAP);

FileStream fs = File.Open(p, FileMode.Open);

fs.Write(b, 0, b.Length);

fs.Close();



Thanks.
 
J

John Wood

Right, hence 'binary' formatter. The binary formatter serializes objects
into a highly efficient format that is not, and should not be,
human-readable.

If you don't care about speed so much and want your files to be
human-readable, then use the soap formatter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top