473,385 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Binaryformatter, unreadable junk

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.
Nov 16 '05 #1
1 1833
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
"2G" <2G@pandora.be> wrote in message
news:OP*************@TK2MSFTNGP10.phx.gbl...
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.

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dominic | last post by:
Hi everybody, In my application, I'm planning to use BinaryFormatter to serialize a potentially huge object to file (and, of course, deserialize back to memory later). My question is if there is...
5
by: aladdinm1 | last post by:
Hi All, I have an annoying trouble with binary serialization. I have a windows forms application which works like a server and keeps sending data to its clients. The data is serialized before...
3
by: Joshua Moore | last post by:
I have a webservice that serializes a ton of variables and other good stuff to a txt file using SoapFormatter (IFormatter), and when I try to deserialize it using the binary formatter, i get the...
0
by: Fred Heida | last post by:
Hi Al, i have a funny problem.. i you can call it funny.. what i have is 2 assemblies, the first one does nothing other then Application.Run(new MyForm())
11
by: Igor | last post by:
Hi. While executing BinaryFormatter.Deserialize() I get: System.InvalidCastException: Specified cast is not valid. I implemented ISerializable interface. What may be a problem? Thanks.
0
by: Fruber Malcome | last post by:
I'm getting a very weird exception and hoping someone may be able to help. I have an Office Add-In that lives in a .dll (for email reference ai.dll) ai.dll makes calls into the core part of the...
19
by: Sharon | last post by:
Hi, When I'm doing BinaryFormatter.Deserialize() over a TCP socket. When I'm closing the TcpListener by invoking the TcpListener.Stop(); I get: System.IO.IOException with message "Unable to...
2
by: Doug Lind | last post by:
Hi all, I have seen a number of posts re: the BinaryFormatter version incompatibility but nothing on how to recover from it. In my case, I want the exception to trigger an alternate behaviour...
11
by: JZ | last post by:
Hi, I'm using a class and binary formatter to store data in files. For example.. Dim FPs As New StuctureDataFile() Dim FileStream As Stream = File.Open(pfile, FileMode.Open) Dim...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.