C# – XML Serialization

This exaple shows how to serialize class to XML. XmlSerializer is used. Notice how attributes are used to avoid exceptions and format the output file.

namespace SimpleExamples
{
    static class SerializationIssue
    {
        public static void Serialize()
        {
            var config = new Config();
            config.Person1 = new People.Mx.Person() { Name = "John", BirthYear = 2000, Profession="SalesManager" };
            config.Person2 = new People.Ax.Person() { Name = "Adam", BirthYear = 1950, Profession="SoftwareDeveloper" };

            var path = Path.GetTempFileName();
            using (var s = new FileStream(path, FileMode.Create))
            {
                new XmlSerializer(typeof(Config)).Serialize(s, config);

                Console.WriteLine(s.Length);
                Process.Start(path);
            }

            var reader = File.OpenRead(path);
            var deserializedConfig = new XmlSerializer(typeof(Config)).Deserialize(reader) as Config;
        }
    }

    [Serializable]
    //sets namespace and name of the root element
    [XmlRoot(ElementName ="Configuration", Namespace ="Configurations")] 
    public class Config
    {
        [XmlElement(ElementName ="PersonA")] //sets the name of Person1 element
        public People.Mx.Person Person1 { get; set; }

        [XmlElement(ElementName ="PersonB")] //sets the name of Person1 element
        public People.Ax.Person Person2 { get; set; }

        [XmlIgnore] //this element won't be serialized
        public int X { get; set; }
    }
}

namespace People.Mx
{
    [Serializable]
    //allows to serialize many classes with the same name but in different namespaces
    //without this attribute on both Person classes exception is thrown during serialization
    [XmlType(TypeName = "Prsn1")] 
    public class Person : PersonBase
    {
        public string Profession { get; set; }
    }
}

namespace People.Ax
{
    [Serializable]
    //allows to serialize many classes with the same name but in different namespaces
    //without this attribute on both Person classes 
    //exception is thrown during serialization[XmlType(TypeName = "Prsn1")] 
    [XmlType(TypeName = "Prsn2")]
    public class Person : PersonBase
    {
        public string Profession { get; set; }
    }
}

namespace People.Base
{
    public class PersonBase
    {
        public string Name { get; set; }
        public int BirthYear { get; set; }
    }
}

For more about Equals method see:
https://msdn.microsoft.com/en-us/library/182eeyhh(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.110).aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s