Click here to Skip to main content
Click here to Skip to main content

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);
        }
    }
}

License

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: http://rashimuddin.wordpress.com/
 
My Email: rashimiiuc at yahoo dot com
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 4 [modified] PinmemberMyosotis Arae6-Aug-13 1:30 
GeneralRe: My vote of 4 PinmemberMd. Rashim uddin21-Sep-13 21:40 
GeneralGood tutorial My vote 4 PinmemberAshish Ramteke3-Jun-13 2:48 
GeneralRe: Good tutorial My vote 4 PinmemberMd. Rashim uddin3-Jun-13 5:40 
GeneralMy vote of 4 PinmemberNamazi23-Apr-13 21:51 
Questionquestion PinmemberafroDeluXe31-Oct-12 5:57 
GeneralMy vote of 2 Pinmemberbobfox12-May-12 9:51 
QuestionGood Job PinmemberSaiyed Alam18-Mar-12 20:29 
AnswerRe: Good Job PinmemberMd. Rashim uddin18-Mar-12 20:40 
GeneralMy vote of 5 Pinmemberenamur18-Mar-12 20:04 
GeneralRe: My vote of 5 PinmemberMd. Rashim uddin18-Mar-12 20:41 
GeneralMy vote of 4 PinmemberJasmin akther Suma17-Mar-12 5:53 
GeneralRe: My vote of 4 PinmemberMd. Rashim uddin17-Mar-12 17: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 | Mobile
Web04 | 2.8.140718.1 | Last Updated 12 May 2012
Article Copyright 2012 by Md. Rashim uddin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid