Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Achieve Persistence Through Serialization

, 6 Jan 2011
This article compares the two common types of serialization in aspects of data access, readability, and runtime cost.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Trestan
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                BuildinType test1 = new BuildinType();
                Utility.SerializeToXml(test1, "test1.xml");
                BuildinType test2 = (BuildinType)Utility.DeSerializeFromXml(typeof(BuildinType), "test1.xml");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurs: " + ex.Message + "\n");
            }         

            try
            {
                #region Test Case 1
                ///Test Case 1: find the output file in the executing path and open it either in IE or a text editor.
                ///Test result: The content should be human-readable.
                ///
                #endregion
                DerivedClass test1 = new DerivedClass();
                //DerivedClass test1 = new DerivedClass(new Version(3,0));  // Uncomment to run Test Case 8.
                test1.TestFont = new System.Drawing.Font("Arial", 12F); 
                Utility.SerializeToXml(test1, "test2.xml");
                DerivedClass test2 = (DerivedClass)Utility.DeSerializeFromXml(typeof(DerivedClass), "test2.xml");
                /// Test Case 10: After make changes in DerivedClass, place a break point to view the value of test2 object in debugger visualizer.
                /// Check the value of test2.testFont. which is the default value during initialization, not the value assigned above.
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurs: " + ex.Message + "\n");
            }         

            long tick = DateTime.Now.Ticks;
            try
            {
                CollectionTypes test0 = new CollectionTypes();
                Utility.SerializeToXml(test0, "test3.xml");
                test0.Dispose();
                CollectionTypes test2 = (CollectionTypes)Utility.DeSerializeFromXml(typeof(CollectionTypes), "test3.xml");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurs: " + ex.Message + "\n");
            }         

            Console.WriteLine("Time used on a single instance of CollectionTypes (seconds): " + (DateTime.Now.Ticks - tick) / 10000000F +"\n");
            tick = DateTime.Now.Ticks;

            #region Test Case 9
            /// Test Case 9: Run the program and pay attention to the "time used" value in console window. Find the output file test4.dat.
            /// Action: Compare the number with Test Case 16.
            /// Test Result: The time taken to serialize the collection to string format is double or triple the time used by binary format.
            /// Also the file size is 10 times larger.
            /// 
            #endregion
            try
            {
                Console.WriteLine("Test of an array containing 10 elements:");
                CollectionTypes[] test1 = new CollectionTypes[10];
                for (int i = 0; i < 10; i++)
                {
                    test1[i] = new CollectionTypes();
                }

                Console.WriteLine("Time used to create object (seconds): " + (DateTime.Now.Ticks - tick) / 10000000F);
                
                Utility.SerializeWithEncrypt(test1, "test4.dat");
                CollectionTypes[] test2 = (CollectionTypes[])Utility.DeSerializeWithDecrypt(typeof(CollectionTypes[]), "test4.dat");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurs: " + ex.Message + "\n");
            }         

            Console.WriteLine("Time used total (seconds): " + (DateTime.Now.Ticks - tick) / 10000000F);
            Console.Read();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

About the Author

trestan
Team Leader
Canada Canada
Looking for something to do in the new year.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 6 Jan 2011
Article Copyright 2011 by trestan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid