Click here to Skip to main content
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);


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
Follow on   Twitter   LinkedIn

Comments and Discussions

GeneralMy vote of 4 PinmemberJasmin akther Suma17-Mar-12 6:53 
GeneralRe: My vote of 4 PinmemberMd. 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
Web03 | 2.8.150224.1 | Last Updated 12 May 2012
Article Copyright 2012 by Md. Rashim uddin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid