Click here to Skip to main content
15,886,519 members
Articles / Web Development / ASP.NET
Tip/Trick

XML Serialization and Deserialization in C#

Rate me:
Please Sign up or sign in to vote.
1.94/5 (5 votes)
30 May 2012CPOL1 min read 59.7K   12   3
Use System.Xml.Serialization.XmlSerializer to perform XML serialization and deserialization.

Introduction 

Serialization is the process of converting the state of an object into a form that can be persisted in a storage medium or transported across the processes/machines. Deserialization is the opposite of serialization, which is a process that converts the outcome of serialization into the original object. The .NET Framework offers two serialization technologies, binary and XML. 

In this article, we will discuss XML Serialization. We use System.Xml.Serialization.XmlSerializer to perform XML serialization and deserialization.

Using the code 

The following method can be used to serialize an object and store the resulting xml to a file.  

This is a generic helper method which takes object of type T and a filename as a arguments and serializes the object to the specified filename. You can pass an object of type T as an argument or complex object like object of type List as a first argument to this method. The output is a boolean value of successful completion of Serialization process and file containing the XML data. 

C#
public static bool Serialize<t>(T value, String filename)
{
    if (value == null)
    {
        return false;
    }
    try
    {
        XmlSerializer _xmlserializer = new XmlSerializer(typeof(T));
        Stream stream = new FileStream(filename, FileMode.Create);
        _xmlserializer.Serialize(stream, value);
        stream.Close();
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

To deserialize an XML file, we use the following generic method. This method takes the XML filename as an argument and desrializes the XML file and return the object of type T

C#
public static T Deserialize<t>(String filename)
{
    if (string.IsNullOrEmpty(filename))
    {
        return default(T);
    }
    try
    {
        XmlSerializer _xmlSerializer = new XmlSerializer(typeof(T));
        Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
        var result = (T)_xmlSerializer.Deserialize(stream);
        stream.Close();
        return result;
    }
    catch (Exception ex)
    {
        return default(T);
    }
}

License

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


Written By
Software Developer Texas Learning and Computation Center
United States United States
Darshan is an enthusiastic and passionate Software Developer. He has achieved a Master's Degree in Computer Science from University of Houston, TX. He currently works as a Research Assistant at Texas Learning and Computation Center. He owns this blog and loves sharing interesting thoughts about programming.

Visit my personal Blog, www.darshansblog.com

Comments and Discussions

 
GeneralSo, What is the point? Pin
Behzad Sedighzadeh29-Jan-16 5:21
Behzad Sedighzadeh29-Jan-16 5:21 
QuestionXML Serialization Pin
Brian Daniels10-Dec-14 12:20
Brian Daniels10-Dec-14 12:20 
QuestionReturn new Instance Pin
Tommy073-Jun-12 22:12
Tommy073-Jun-12 22:12 

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

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