Groups | Blog | Home
all groups > c# > june 2004 >

c# : Binaryformatter, unreadable junk


John Wood
6/24/2004 5:53:02 PM
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.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
[quoted text, click to view]

2G
6/24/2004 11:00:09 PM
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.

AddThis Social Bookmark Button