Click here to Skip to main content
13,865,159 members
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 16 Mar 2012
Licenced CPOL

XML Serialization and Deserialization

, 12 May 2012
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);


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


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:

My Email: rashimiiuc at yahoo dot com

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Myosotis Arae6-Aug-13 2:30
memberMyosotis Arae6-Aug-13 2:30 
GeneralRe: My vote of 4 Pin
Md. Rashim uddin21-Sep-13 22:40
memberMd. Rashim uddin21-Sep-13 22:40 
GeneralMy vote of 4 Pin
Namazi23-Apr-13 22:51
memberNamazi23-Apr-13 22:51 
Questionquestion Pin
afroDeluXe31-Oct-12 6:57
memberafroDeluXe31-Oct-12 6:57 
GeneralMy vote of 2 Pin
bobfox12-May-12 10:51
professionalbobfox12-May-12 10:51 
QuestionGood Job Pin
Saiyed Alam18-Mar-12 21:29
memberSaiyed Alam18-Mar-12 21:29 
AnswerRe: Good Job Pin
Md. Rashim uddin18-Mar-12 21:40
memberMd. Rashim uddin18-Mar-12 21:40 
GeneralMy vote of 5 Pin
enamur18-Mar-12 21:04
memberenamur18-Mar-12 21:04 
GeneralRe: My vote of 5 Pin
Md. Rashim uddin18-Mar-12 21:41
memberMd. Rashim uddin18-Mar-12 21:41 
GeneralMy vote of 4 Pin
Jasmin akther Suma17-Mar-12 6:53
memberJasmin akther Suma17-Mar-12 6:53 
GeneralRe: My vote of 4 Pin
Md. Rashim uddin17-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    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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 12 May 2012
Article Copyright 2012 by Md. Rashim uddin
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid