Click here to Skip to main content
Click here to Skip to main content

Tagged as

C# Serialisation - The Best Way

, 20 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
C# Serialisation - The Best Way
Serialisation - The Common Way
 
The most common way you find on the internet of serialising objects to streams in C# is the XmlSerialiser. Now, while this is a very good class to use it does have quite significant problems including the fact that it cannot serialise things such as color, font, timespan, etc. However, there is a better and often easier way of doing serialisation (that also doesn't require all the nasty XmlInclude attributes)...
 
Binary Formatter
 
The BinaryFormatter class is under the namespace: System.Runtime.Serialisation.Formatters.Binary.
 
(It's no wonder hardly anyone finds it... Poke tongue | ;-P )
 
Anyway, it is far better as not only can it serialise any object (so far as I know) but you also don't need to; Specify type you are going to serialise/deserialise; Have XmlInclude, XmlIgnore and other such tags (nor have extra members of classes just to get things to serialise properly) in fact, all you need is the Serialisable attribute on any class you wish to serialise. I'll give a simple example:
 
namepsace Test
{
    //The Serialisable attribute
    [Serialisable]
    class TestClass
    {
        public Color AColor = Color.Black;
        public TestClass()
        {
        }
 
        public static void Serialise(string Filename, TestClass TheObj)
        {
            //Just to prove that it can serialise colors, set this to 
            //something other than the default so that we can tell if it worked after deserialising.
            TheObj.AColor = Color.Red;
 
            BinaryFormatter Ser = new BinaryFormatter();
            FileStream TheStream = new FileStream(Filename);
            Ser.Serialize(TheStream, TheObj);
            TheStream.Close();
        }
 
        public static TestClass Deserialise(string Filename)
        {
            TestClass RetClass = null;
 
            BinaryFormatter Ser = new BinaryFormatter();
            FileStream TheStream = new FileStream(Filename);
            RestClass = (TestClass)(Ser.Deserialize(TheStream));
 
            return RestClass;
        }
    }
}
 
So you can see (hopefully) that this is better than XmlSerialisation and can be adapted for any object and you don't need to use FileSTream either if you don't want to. I use a similar thing to the above but using a MemoryStream to serialise messages sent over a network. BinaryFormatter is a far more powerful and, so far as I can tell, faster class that cuts out a lot of the XmlSerialiser mess. Big Grin | :-D

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ed Nutting
Student
United Kingdom United Kingdom
Hi,
 
I am Edward Nutting, born March 1996 and I am currently an Electrical and Electronic Engineering undergraduate at the University of Bristol, UK. I've been self-teaching programming since I was eight years old and have dabbled in many different areas from web development (where I had my own advertising startup) to low level operating system stuff (my current project Fling OS).
 
The articles and code on this profile are some of my early work (from when I was aged about 13 to 16) but despite my young age, it has been well received by CodeProject users. My more recent work has been for companies or on projects hosted on GitHub, CodePlex or BitBucket.
 
I keep an eye on many things techy and am open to any and all ideas, so please feel free to contact me if you think I can help! Smile | :)
 
Cheers,
Ed Nutting
Follow on   Twitter   LinkedIn

Comments and Discussions

 
Questionhi PinmemberNikos1722-Nov-12 0:35 
GeneralYes true this is only basic but as a useful tip BinaryFormat... PinmemberEdMan19621-Mar-11 8:13 
GeneralThis provides the basics for the serialization concept. Othe... PinmemberNitinSingh20-Mar-11 22:44 
GeneralDanger! BinaryStream format is not persistent. PinmemberCorvus Corax21-Mar-11 17:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 20 Mar 2011
Article Copyright 2011 by Ed Nutting
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid