Click here to Skip to main content
15,894,460 members
Articles / Programming Languages / C#

Comparing .NET XML Serializers: Part One

Rate me:
Please Sign up or sign in to vote.
4.56/5 (8 votes)
30 Jul 2010Apache5 min read 39.2K   279   28  
What serializer is the best for saving application state?
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Media;
using System.Xml;
using XmlSerializersTest.Samples;
using XmlSerializersTest.Serializers;
using System.Reflection;

namespace XmlSerializersTest
{
    class Program
    {
        static void Main()
        {
            SetupAssemblyResolver();

            using (var writer = new XmlTextWriter("result.xml", Encoding.UTF8))
            {
                writer.Formatting = Formatting.Indented;
                var binPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                var samplePath = Path.Combine(binPath, @"..\..\samples");
                var serializers = new IXmlSerializer[]
                                      {
                                          new StandardXmlSerializer(), 
                                          new XamlSerializer(),
                                          new WcfSerializer(false),
      
                                    new WcfSerializer(true)
                                      };
                using (var processor = new SampleProcessor(samplePath, serializers, writer))
                {
                    ProcessSamples(processor);
                }
            }
        }

        private static void SetupAssemblyResolver()
        {
            AppDomain.CurrentDomain.AssemblyResolve += HelperAssemblyLoader.AssemblyResolve;
        }

        private static void ProcessSamples(SampleProcessor processor)
        {
            processor.Process(new EmptyClass());
            processor.Process(new OneSimpleProperty { Value = 42 });
            processor.Process(new ColorProperty { Color = Colors.Red });
            processor.Process(new NullProperty());
            processor.Process(new GenericList { Strings = new List<string> {"foo", "bar", "baz"} });
            processor.Process(new GenericListXamlContent { Strings = new List<string> { "foo", "bar", "baz" } });
            processor.Process(new GenericListXamlContentPrivateSetter());
            processor.Process(new GenericDictionary { Things = new Dictionary<int, string> { { 1, "one" }, { 2, "two" } } });
            processor.Process(new GenericDictionaryFixNull { Things = new Dictionary<int, string> { { 1, "one" }, { 2, "two" } } });
            processor.Process(new GenericDictionaryCustomKey
                {
                    Things = new Dictionary<OneSimpleProperty, string> { { new OneSimpleProperty { Value = 1 }, "one" } }
                });
            processor.Process(new GenericDictionaryCustomValue
            {
                Things = new Dictionary<string, OneSimpleProperty> { { "one", new OneSimpleProperty { Value = 1 } } }
            });
            processor.Process(ObjectGraph.GetSampleGraph());
            processor.Process(ObjectGraphWithCycle.GetSampleGraphWithCycle());
            processor.Process(new XmlSerializable() { Field = "test" });
            processor.Process(new IgnoreProperty());
            processor.Process(GetMissingPropertyV1Instance(), GetMissingPropertyV2Type());
        }

        private static object GetMissingPropertyV1Instance()
        {
            var type = Type.GetType("XmlSerializersTest.Samples.MissingProperty, HelperAssembly, Version=1.0.0.0");
            var method = type.GetMethod("GetInstance", BindingFlags.Public | BindingFlags.Static);
            return method.Invoke(null, null);
        }

        private static Type GetMissingPropertyV2Type()
        {
            return Type.GetType("XmlSerializersTest.Samples.MissingProperty, HelperAssembly, Version=2.0.0.0");
        }
    }
}

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 Apache License, Version 2.0


Written By
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions