Click here to Skip to main content
15,889,462 members
Articles / Programming Languages / C# 4.0
Tip/Trick

XML Serialization and Deserialization

Rate me:
Please Sign up or sign in to vote.
4.68/5 (25 votes)
12 May 2012CPOL 62.2K   47   12
XML Serialization and Deserialization in c#

Object Serialization, which is also called deflating or marshaling, is a progression through which an object’s state is converted into some serial data format, such as XML or binary format, in order to be stowed for some advanced use whereas Deserialization is the opposite process of Serialization which is also called inflating or unmarshalling. The code showing the very simple way for serializing and deserializing from XML to any Object and vice versa as well,

Use this method for serialization,

C#
public static string Serialize<T>(T value)
{
    if (value == null)
    {
        return null;
    }

    XmlSerializer serializer = new XmlSerializer(typeof(T));
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = new UnicodeEncoding(false, false);
    settings.Indent = false;
    settings.OmitXmlDeclaration = false;

    using (StringWriter textWriter = new StringWriter())
    {
        using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
        {
            serializer.Serialize(xmlWriter, value);
        }
        return textWriter.ToString();
    }
}

Use this method for Deserialization:

C#
public static T Deserialize<T>(string xml)
{
    if (string.IsNullOrEmpty(xml))
    {
        return default(T);
    }

    XmlSerializer serializer = new XmlSerializer(typeof(T));
    XmlReaderSettings settings = new XmlReaderSettings();

    using (StringReader textReader = new StringReader(xml))
    {
        using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
        {
            return (T)serializer.Deserialize(xmlReader);
        }
    }
}

License

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


Written By
Chief Technology Officer RightKnack Limited
Bangladesh Bangladesh
A big fan of getting down the latest cutting-edge technologies to the ground to innovate exceptionally amazing ideas.

My Blog: http://rashimuddin.wordpress.com/

My Email: rashimiiuc at yahoo dot com

Comments and Discussions

 
GeneralMy vote of 4 Pin
Myosotis Arae6-Aug-13 1:30
Myosotis Arae6-Aug-13 1:30 
GeneralRe: My vote of 4 Pin
Md. Rashim Uddin21-Sep-13 21:40
Md. Rashim Uddin21-Sep-13 21:40 
Thanks for your trying up with that code. I am giving you a piece of exapmles,

Say the data structure is like that,

C#
public class MyClass
{
   public MyClass()
   {
      Children = new List<MyClass>();
   }
   public string Name { get; set; }
   public List<MyClass> Children { get; set; }
}


so if you want to serialize you just need to call the Serialize method,

C#
var children = new MyClass()
                                {
                                    Name = "Child 1"
                                };

var myClass = new MyClass {Name = "Test"};
myClass.Children.Add(children);

var serialize = SerializationHelper.Serialize(myClass);

the output will be like that,

XML
<?xml version="1.0" encoding="utf-16"?>
<MyClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Name>Test</Name>
  <Children>
    <MyClass>
      <Name>Child 1</Name>
      <Children />
    </MyClass>
  </Children>
</MyClass>

And for Deserialization,

C#
var deserialize = SerializationHelper.Deserialize<MyClass>(serialize);


I guess you have got your answer.

Thanks
GeneralMy vote of 4 Pin
Namazi23-Apr-13 21:51
Namazi23-Apr-13 21:51 
Questionquestion Pin
afroDeluXe31-Oct-12 5:57
afroDeluXe31-Oct-12 5:57 
GeneralMy vote of 2 Pin
bobfox12-May-12 9:51
professionalbobfox12-May-12 9:51 
QuestionGood Job Pin
Saiyed Alam18-Mar-12 20:29
Saiyed Alam18-Mar-12 20:29 
AnswerRe: Good Job Pin
Md. Rashim Uddin18-Mar-12 20:40
Md. Rashim Uddin18-Mar-12 20:40 
GeneralMy vote of 5 Pin
enamur18-Mar-12 20:04
enamur18-Mar-12 20:04 
GeneralRe: My vote of 5 Pin
Md. Rashim Uddin18-Mar-12 20:41
Md. Rashim Uddin18-Mar-12 20:41 
GeneralMy vote of 4 Pin
Jasmin akther Suma17-Mar-12 5:53
Jasmin akther Suma17-Mar-12 5:53 
GeneralRe: My vote of 4 Pin
Md. Rashim Uddin17-Mar-12 17:50
Md. Rashim Uddin17-Mar-12 17:50 

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.