brent

brent


  • Name: [not set]
  • Favorite Languages: [not set]
  • Website: [not set]
  • Location: [not set]
  • About Me: [not set]

Recent Comments

  • problem with deserialization
    06/09/2009 - 12:47

    Well there she blows!
    Thanks, that fixed the problem.

    Oh well, it's the simple mistakes that are hardest to bare. If you could see my face, I guess you could call me 'the reddest' right now.

    Do you think that the serialisation mechanism is a bit complicated? Not so much in the code needed to make it work but the content of the data files it produces?

    Yesterday, in preparing to reply to your response, I tried several variants - 2 entries, 3 entries, 4 entries and so on. I spent some time using Debug.exe to view the file content. Within the data file, there are all sorts of entries associated with the application as well as the serialised data that I did not expect. Is there any way to force the mechanism to simplify its output? I cannot help thinking that if I had a data loss or a corruption problem at some point I would find it very hard to recover the data.

    I hope you will be willing to discuss. But again, many thanks for your help and the tutorial.

  • problem with deserialization
    06/08/2009 - 15:53

    Yes it is, and the content seems reasonable. The car and owner strings are clear to see even with Notepad.

    When it runs I get,

    enter command >i
    initialised
    Entries =2
    0 Saturn0,Sky0,2007
    1 Nissan0,Maxima0,2006

    enter command >i
    initialised
    Entries =4
    0 Saturn0,Sky0,2007
    1 Nissan0,Maxima0,2006
    2 Saturn1,Sky1,2008
    3 Nissan1,Maxima1,2007

    okay, let save the data.

    enter command >s
    saving
    document serialising
    TCAR serialising
    TOWNER serialising
    TCAR serialising
    TOWNER serialising
    TCAR serialising
    TOWNER serialising
    TCAR serialising
    TOWNER serialising
    Entries =4
    0 Saturn0,Sky0,2007
    1 Nissan0,Maxima0,2006
    2 Saturn1,Sky1,2008
    3 Nissan1,Maxima1,2007
    enter command >q
    quit

    ..this creates 4 car entries and then writes to data3.txt

    When I run the application again and attempt to load data from data3.txt I get,

    enter command >l
    loading
    document deserialising
    TOWNER deserialising
    TOWNER deserialising
    TOWNER deserialising
    TOWNER deserialising
    TCAR deserialising
    TCAR deserialising
    TCAR deserialising
    TCAR deserialising
    Entries =0
    enter command >q
    quit

    It's making all the right calls to the deserialise functions, but the end result is an empty list.

  • C# Tutorial - Serialize Objects to a File
    06/04/2009 - 06:48

    Hi, I've tried the original tutorial code. I seem to have a problem during deserialization.

    I've collected all the tutorial source code snippets together to create the enclosed console based application.

    The application provides 3 commands i,s,l

    To create the scenario run code.exe and type the following,
    i ..this creates 2 entries
    i ..this creates 2+2 entries
    s .. saves the binary data into data3.txt
    q .. quits the console application

    now run code.exe a second time
    l .. this loads data3.txt

    At this point I would expect to see 4 car entries but the console debug indicates that the cars list has zero entries after deserialization. Something obvious is missing in the code but I am a novice to c# so I'm stumped.

    The source is contained in a single file called code.cs - see below.

    I'm using the microsoft c# command line compiler, csc. So, from the dos command line,
    to compile, type:- csc code.cs
    to run, type:- code.exe

     

    //code.cs

    //based on c# tutorial - serialize objects to a file by The Reddest
    //goto http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file


    //Program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using mydata;
    using serializers;

    //extra bits to make the single file version compile
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;


    namespace filer3
    {
        class Program
        {

            static void setup(List<TCar> list, int n)
            {
                string N = Convert.ToString(n);
                TCar car1 = new TCar();
                TOwner owner1 = new TOwner();
                list.Add(car1);

                car1.owner = owner1;
                car1.make = "Saturn" + N;
                car1.model = "Sky" + N;
                car1.year = 2007 + n;
                owner1.firstName = "The" + N;
                owner1.lastName = "Reddest" + N;

                TCar car2 = new TCar();
                TOwner owner2 = new TOwner();
                list.Add(car2);

                owner2.firstName = "The" + N;
                owner2.lastName = "Tallest" + N;
                car2.owner = owner2;
                car2.make = "Nissan" + N;
                car2.model = "Maxima" + N;
                car2.year = 2006 + n;
            }


            static void Main(string[] args)
            {
                int j = 0;
                List<TCar> cars = new List<TCar>();
                TDocument document = new TDocument();
                TFiler filer = new TFiler();
                TCar xx;
                string fname = "data3.txt";
                document.Cars = cars;

                while (1 == 1)
                {

                    Console.Write("enter command >");
                    string line = Console.ReadLine();
                    if (line == "i")
                    {
                        Console.WriteLine("initialised");
                        setup(document.Cars, j++);
                    }
                    if (line == "l")
                    {
                        Console.WriteLine("loading");
                        filer.DeSerializeObject(fname);
                    }
                    if (line == "s")
                    {
                        Console.WriteLine("saving");
                        filer.SerializeObject(fname, document);
                    }
                    if (line == "q")
                    {
                        Console.WriteLine("quit");
                        break;
                    }


                    Console.WriteLine("Entries =" + Convert.ToString(document.Cars.Count));
                    for (int n = 0; n < document.Cars.Count; n++)
                    {
                        xx = document.Cars.ElementAt(n);
                        Console.Write(Convert.ToString(n) + "  ");
                        Console.Write(xx.make + ",");
                        Console.Write(xx.model + ",");
                        Console.WriteLine(xx.year);
                    }
                }
            }
        }
    }
    //TCar.cs
    //using System;
    //using System.IO;
    //using System.Runtime.Serialization;

    namespace mydata
    {
        [Serializable()]
        public class TCar:ISerializable
        {
            public string make;
            public string model;
            public int year;
            public TOwner owner;

            public TCar()
            {
            }        
            public TCar(SerializationInfo info, StreamingContext ctxt)
            {
                Console.WriteLine("TCAR deserialising");
                this.make = (string)info.GetValue("Make", typeof(string));
                this.model = (string)info.GetValue("Model",typeof(string));
                this.year = (int)info.GetValue("Year", typeof(int));
                this.owner = (TOwner)info.GetValue("Owner", typeof(TOwner));
            }
            public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
            {
                Console.WriteLine("TCAR serialising");
                info.AddValue("Make", this.make);
                info.AddValue("Model", this.model);
                info.AddValue("Year", this.year);
                info.AddValue("Owner", this.owner);            
            }      
        }
    }
    //TDocument.cs
    //using System;
    //using System.IO;
    //using System.Runtime.Serialization;
    //using System.Collections;
    //using System.Collections.Generic;
    //using System.Runtime.Serialization.Formatters.Binary;
    //using mydata;


    namespace mydata
    {

        [Serializable()]
        // Top level data object all other data types are encapsulated by this class
        public class TDocument : ISerializable
        {
            private List<TCar> cars;

            public  List<TCar> Cars
            {
                get { return this.cars; }
                set { this.cars = value; }
            }

            public TDocument()
            {
            }

            public TDocument(SerializationInfo info, StreamingContext ctxt)
            {
                Console.WriteLine("document deserialising");
                this.cars = (List<TCar>)info.GetValue("Cars", typeof(List<TCar>));
            }

            public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
            {
                Console.WriteLine("document serialising");
                info.AddValue("Cars", this.cars);
            }
        }
    }

    //TOwner.cs
    //using System;
    //using System.Collections.Generic;
    //using System.Linq;
    //using System.Text;

    //using System.IO;
    //using System.Runtime.Serialization;
    //using System.Runtime.Serialization.Formatters.Binary;


    namespace mydata
    {
        [Serializable()]
        public class TOwner : ISerializable
        {
            public string firstName;
            public string lastName;

            public TOwner()
            {
            }
            public TOwner(SerializationInfo info, StreamingContext ctxt)
            {
                Console.WriteLine("TOWNER deserialising");
                this.firstName = (string)info.GetValue("FirstName", typeof(string));
                this.lastName =  (string)info.GetValue("LastName",  typeof(string));
            }
            public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
            {
                Console.WriteLine("TOWNER serialising");
                info.AddValue("FirstName", this.firstName);
                info.AddValue("LastName", this.lastName);
            }
        }
    }
    //TSerialiser_1.cs
    //using System;
    //using System.IO;
    //using System.Runtime.Serialization;
    //using System.Runtime.Serialization.Formatters.Binary;
    //using mydata;

    namespace serializers
    {
        public class TFiler
        {
            //Version 1 serialiser, instance specific, so a bit of a pain

            public void SerializeObject(string filename, TDocument document)
            {
                Stream stream = File.Open(filename, FileMode.Create);  
                BinaryFormatter bFormatter = new BinaryFormatter();
                bFormatter.Serialize(stream, document);
                stream.Close();
            }

            public TDocument DeSerializeObject(string filename)        
            {
                TDocument document;
                Stream stream = File.Open(filename, FileMode.Open);
                BinaryFormatter bFormatter = new BinaryFormatter();
                document = (TDocument)bFormatter.Deserialize(stream);
                stream.Close();
                return document;
            }
        }
    }