Click here to Skip to main content
11,412,578 members (73,760 online)
Click here to Skip to main content

XML Serialization and Deserialization

, 12 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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,

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:

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)

Share

About the Author

Md. Rashim uddin
Software Developer (Senior) KAZ Software Limited.
Bangladesh Bangladesh
I am a true visionary as well as have always been passionate about learning. I always love to work with thought-provoking personnel to develop amazing, exciting and innovative ideas as part of an aggressive team that challenges my ingenious ideas, creativeness and extensive experience.

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

My Email: rashimiiuc at yahoo dot com
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 4 [modified] Pin
Myosotis Arae at 6-Aug-13 2:30
memberMyosotis Arae6-Aug-13 2:30 
GeneralRe: My vote of 4 Pin
Md. Rashim uddin at 21-Sep-13 22:40
memberMd. Rashim uddin21-Sep-13 22:40 
GeneralGood tutorial My vote 4 Pin
Ashish Ramteke at 3-Jun-13 3:48
memberAshish Ramteke3-Jun-13 3:48 
GeneralRe: Good tutorial My vote 4 Pin
Md. Rashim uddin at 3-Jun-13 6:40
memberMd. Rashim uddin3-Jun-13 6:40 
GeneralMy vote of 4 Pin
Namazi at 23-Apr-13 22:51
memberNamazi23-Apr-13 22:51 
Questionquestion Pin
afroDeluXe at 31-Oct-12 6:57
memberafroDeluXe31-Oct-12 6:57 
GeneralMy vote of 2 Pin
bobfox at 12-May-12 10:51
memberbobfox12-May-12 10:51 
QuestionGood Job Pin
Saiyed Alam at 18-Mar-12 21:29
memberSaiyed Alam18-Mar-12 21:29 
AnswerRe: Good Job Pin
Md. Rashim uddin at 18-Mar-12 21:40
memberMd. Rashim uddin18-Mar-12 21:40 
GeneralMy vote of 5 Pin
enamur at 18-Mar-12 21:04
memberenamur18-Mar-12 21:04 
GeneralRe: My vote of 5 Pin
Md. Rashim uddin at 18-Mar-12 21:41
memberMd. Rashim uddin18-Mar-12 21:41 
GeneralMy vote of 4 Pin
Jasmin akther Suma at 17-Mar-12 6:53
memberJasmin akther Suma17-Mar-12 6:53 
GeneralRe: My vote of 4 Pin
Md. Rashim uddin at 17-Mar-12 18:50
memberMd. Rashim uddin17-Mar-12 18:50 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.1 | Last Updated 12 May 2012
Article Copyright 2012 by Md. Rashim uddin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid