[Mono-dev] Problem with BinarySerialization

PFJ pjohnson1 at uclan.ac.uk
Tue Feb 2 07:04:31 EST 2010


Hi,

I've created my BinarySerialized file like this...

namespace elements
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stream stream = File.Open("elements.ele", FileMode.Create);
            Elements mp = new Elements();
           
            string[] elements = new string[111] {"H","He",
						"Li","Be","B","C","N","O","F","Ne",
						"Na","Mg","Al","Si","P","S","Cl","Ar",
						"K","Ca","Sc","Ti","V", // rest of elements
            string[] names = new string[111] {"Hydrogen", "Helium",
"Lithium", "Berylium", "Boron", "Carbon",
                    "Nitrogen", "Oxygen", "Fluorine", "Neon", // rest of the
element names
            string[] structures = new string[111] {"1s1", "1s2", "[He]2s1",
"[He]2s2", "[He]2s2-2p1",
                    "[He]2s2-2p2", "[He]2s2-2p3", // rest of the atomic
structures
            double[] masses = new double[111] {1.0079,4.0026,
						6.941,9.01218,10.8,12.011,14.0067, // rest of the masses
            BinaryFormatter bformatter = new BinaryFormatter();
            Console.WriteLine("Writing Element Information");
            var elementgo = new List<Elements>();
            for (int m = 0; m < 111; ++m)
            {
                string p = names[m] = ".jpg";
                elementgo.Add(new Elements(names[m], elements[m],
structures[m], p, m + 1, masses[m]));
            }
            bformatter.Serialize(stream, elementgo);
            stream.Close();
        }

       [Serializable()]	//Set this attribute to all the classes that you
define to be serialized
        public class Elements : ISerializable
        {
            public string el_name, el_sym, el_struct, pic;
            public int el_num;
            public double el_mass;

            //Default constructor
            public Elements()
            {
                el_name = null;
                el_sym = null;
                el_struct = null;
                pic = null;
                el_num = 0;
                el_mass = 0.0;
            }

            //Deserialization constructor.
            public Elements(SerializationInfo info, StreamingContext ctxt)
            {
                //Get the values from info and assign them to the
appropriate properties
                el_name = (string)info.GetValue("element_name",
typeof(string));
                el_sym = (String)info.GetValue("element_symbol",
typeof(string));
                el_struct = (string)info.GetValue("element_structure",
typeof(string));
                pic = (string)info.GetValue("picname", typeof(string));
                el_num = (int)info.GetValue("element_number", typeof(int));
                el_mass = (double)info.GetValue("element_weight",
typeof(double));
            }

            //Serialization function.
            public void GetObjectData(SerializationInfo info,
StreamingContext ctxt)
            {
                info.AddValue("element_name", el_name);
                info.AddValue("element_symbol", el_sym);
                info.AddValue("element_structure", el_struct);
                info.AddValue("picname", pic);
                info.AddValue("element_number", el_num);
                info.AddValue("element_weight", el_mass);
            }
    

    public string Name { get; set; }     
    public string Symbol { get; set; }      
    public string Structure { get; set; }
    public string Picture { get; set; }
    public int Number { get; set; }
    public double Mass { get; set; }

    public Elements(string n, string s, string t, string p, int u, double m)
    {
        Name = n;
        Symbol = s;
        Structure = t;
        Picture = p;
        Number = u;
        Mass = m;
    }
}

This happily creates a file called elements.ele - not a problem.

However, the problem comes when I try to read it back in into a different
program. The read in code looks like this

namespace molarity
{	
	[Serializable()]
	
	public class xmlhandler : Form, ISerializable
	{
		public string element_name, element_symbol, element_structure, picname;
		public double element_weight;
		public int element_number;
		
		public xmlhandler()
		{
			element_name = "Beckilium";
			element_symbol = "BBB";
			element_structure = "just-right";
			element_weight = 124.01;
			element_number = 123;
			picname = null;
		}
		
		// deserialization class
		
		public xmlhandler(SerializationInfo info, StreamingContext stream)
		{
			element_name = (string)info.GetValue("element_name", typeof(string));
			element_symbol = (string)info.GetValue("element_symbol", typeof(string));
			element_structure = (string)info.GetValue("element_structure",
typeof(string));
			picname = (string)info.GetValue("picinfo", typeof(string));
			element_number = (int)info.GetValue("element_number", typeof(int));
			element_weight = (double)info.GetValue("element_weight", typeof(double));
		}
		
		// serialization class
		
		public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
		{
			info.AddValue("element_name", element_name);
			info.AddValue("element_symbol", element_symbol);
			info.AddValue("element_structure", element_structure);
			info.AddValue("element_number", element_number);
			info.AddValue("element_weight", element_weight);
			info.AddValue("picname", picname);
		}
		
		public void dotheread()
		{
			xmlhandler elements = new xmlhandler();
			try 
			{
				string path_env = Path.GetDirectoryName(Application.ExecutablePath) +
Path.DirectorySeparatorChar;
				Stream stream = File.Open(path_env + "elements.ele", FileMode.Open);
				BinaryFormatter bf = new BinaryFormatter();
				elements = (xmlhandler)bf.Deserialize(stream);
				stream.Close();
			}
			catch(System.IO.FileNotFoundException)
			{
				MessageBox.Show("Unable to find the elements information file", "File
not found", MessageBoxButtons.OK);
			}
		}

The file is being found, but I'm getting a Deserialize problem and I get the
error

System.Runtime.Serialization.SerializationException: Unable to find assembly
'elements, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Do I need to add this into the references? I would have thought as it is an
external file, it would just be read in via the stream method.

Any ideas?

TTFN

Paul
-- 
View this message in context: http://old.nabble.com/Problem-with-BinarySerialization-tp27419333p27419333.html
Sent from the Mono - Dev mailing list archive at Nabble.com.



More information about the Mono-devel-list mailing list