Diluksha's profileDiluksha's spacePhotosBlogListsMore Tools Help

Blog


    October 09

    XML Serialization with .Net

     

    What is XML Serialization?

    Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream.

    And also serialization of XML to common language runtime objects enables one to convert XML documents into a form where they are easier to process using conventional programming languages.

    Sample with C# .Net

    1. Create a console application

    2. Add new XML schema to the project

    clip_image002

    3. Add new Complex type and an Element type to the schema.

    clip_image004

    4. Run following command in Visual Studio Command Prompt to convert created XML schema to the C# class.

    >xsd.exe /c Person.xsd

    This will create a C# file named “Person.cs” inside the solution folder. Add the created C# file into the solution.

    5. xsd.exe is a utility to generate schema or class files from given source. It is included with the .NET Framework. There are three main uses for xsd.exe.

    · xsd.exe <schema>.xsd which will create source code for the appropriate class to de-serialize your object.

    · xsd.exe <assembly>.dll|.exe is used to read the code and generate a schema (.xsd) file for you.

    · xsd.exe <instance>.xml|.xdr which will try to infer an .xsd file which you can then use to create an appropriate class

    6. Code sample to do a serialization

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Xml.Serialization;
    using System.IO;

    namespace SerializationSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Serializing..
                Person person = new Person();
                person.id = 100;
                person.name = "Diluksha Geeganage";
                person.address = "Sri Lanka";
                person.age = 27;

                XmlSerializer xmlS = new XmlSerializer(typeof(Person));
                FileStream file = new FileStream(@"Persons.xml", FileMode.Create, FileAccess.Write);
                xmlS.Serialize(file, person);

                Console.WriteLine("Serializing Completed");
                Console.Read();

                file.Close();
            }
        }
    }

    7. Code sample to do a deserialization

    using System;
    using System.Collections.Generic;
    using System.Text;

    using System.Xml.Serialization;
    using System.IO;

    namespace SerializationSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Deserializing..
                Person objPerson;

                XmlSerializer xmlD = new XmlSerializer(typeof(Person));
                FileStream fileD = new FileStream(@"Persons.xml", FileMode.Open, FileAccess.Read);

                objPerson = (Person)xmlD.Deserialize(fileD);
                Console.WriteLine("ID {0} Name {1} Address {2} Age {3}", objPerson.id, objPerson.name, objPerson.address, objPerson.age);
                Console.Read();

                fileD.Close();
            }
        }
    }