Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary serialization

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
SE
I am using simple binary serialization and deserialization from this page:
The problem is that I serialize an array in one program and deserializing it in another. This causes the following error:

System.RunTime.Serialization.SerializationException: Cannot find the assembly: (assemblyname of my application which serializes the object).

Why is this happening, can't I use binary serialization over different applications?


--- neteject.com - Internet Solutions ---
 
Serialize(Stream, object);
object Deserialize(Stream);
If the object are System types e.g. ArrayList, Hashtable of predefined types then you can serialize that object in one assembly and deserialize in another without problems by providing the binary file.
Code:
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace firstass
{

class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			Serialize();
			Console.ReadLine();
		}
		static void Serialize() 
		{
			// Create a hashtable of values that will eventually be serialized.
			Hashtable addresses = new Hashtable();
			addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
			addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
			addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");

			// To serialize the hashtable and its key/value pairs,  
			// you must first open a stream for writing. 
			// In this case, use a file stream.
			FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

			// Construct a BinaryFormatter and use it to serialize the data to the stream.
			BinaryFormatter formatter = new BinaryFormatter();
			try 
			{
				formatter.Serialize(fs, addresses);
			}
			catch (SerializationException e) 
			{
				Console.WriteLine("Failed to serialize. Reason: " + e.Message);
				throw;
			}
			finally 
			{
				fs.Close();
			}
		}
To deserialize the DataFile.dat on another machine and in another assembly, just copy there the DataFile.dat and desrialzie :
Code:
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace secondass
{
	class AnotherClass
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Deserialize();
			Console.ReadLine();
		}
		static void Deserialize() 
		{
			// Declare the hashtable reference.
			Hashtable addresses  = null;

			// Open the file containing the data that you want to deserialize.
			FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
			try 
			{
				BinaryFormatter formatter = new BinaryFormatter();

				// Deserialize the hashtable from the file and 
				// assign the reference to the local variable.
				addresses = (Hashtable) formatter.Deserialize(fs);
			}
			catch (SerializationException e) 
			{
				Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
				throw;
			}
			finally 
			{
				fs.Close();
			}

			// To prove that the table deserialized correctly, 
			// display the key/value pairs.
			foreach (DictionaryEntry de in addresses) 
			{
				Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
			}
		}
	}
}
If the object is user type e.g. class MyClass, then you can deserialize in another assembly if that assembly knows the type of the serialized object e.g. MyClass.
-obislavu-

 
The object is user type.

The second project has the same class except for some functions (which should not be serialized). I looked at the serialized file in a texteditor and I saw that the it said something about the the first project assembly.

Any idea why it is complaining when loading the xml-file when the class is the same except for functions?

--- neteject.com - Internet Solutions ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top