473,789 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(objec t o, SerializationFo rmat format) {

IFormatter iFor = null;

switch(format) {

case SerializationFo rmat.BINARY:

iFor = new BinaryFormatter ();

break;

case SerializationFo rmat.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(se tt, SerializationFo rmat.BINARY);

//byte[] b = sl.Serialize(se tt, SerializationFo rmat.SOAP);

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

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

fs.Close();

Thanks.
Nov 16 '05 #1
1 1849
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.b e> wrote in message
news:OP******** *****@TK2MSFTNG P10.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(objec t o, SerializationFo rmat format) {

IFormatter iFor = null;

switch(format) {

case SerializationFo rmat.BINARY:

iFor = new BinaryFormatter ();

break;

case SerializationFo rmat.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(se tt, SerializationFo rmat.BINARY);

//byte[] b = sl.Serialize(se tt, SerializationFo rmat.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
3808
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 any hard limit on the size of this object? Is it only limited by the amount of memory or hard-disk space in the server?
5
3834
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 being sent using BinaryFormatter and a serializable object. When using a windows forms application as a client, everything works just fine and deserialized data can be received intact. When the client application is is embedded in Internet Explorer (I...
3
5482
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 following error: BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 1986938170.1886350437. I've read a lot about this error from several sources on the net, but can't
0
1771
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
3959
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
2078
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 application implementation in another .dll (let's call that app.dll). app.dll makes a function call into another dll (let's call that one dep.dll). dep.dll has some code in it where it deserializes a memorystream using a binaryformatter.
19
6250
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 read data from the transport connection." that InnerException of type System.Net.Sockets.SocketException saying "An established connection was aborted by the software in your host machine".
2
5820
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 and continue process the stream. My app reads and writes configuration settings to a binary file upon starting and shutdown. The settings are stored in various objects that are serialized and deserialized using a BinaryFormatter. When reading...
11
3124
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 FileFormatter As New BinaryFormatter() FPs = DirectCast(FileFormatter.Deserialize(FileStream), StuctureDataFile)
0
9506
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9978
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6755
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5414
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5546
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4087
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.