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

XML Serialization and Deserialization

By , 12 May 2012
 

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
Member
I am nothing more than a simple man with the high vision of evolving multifaceted software systems by using my technical and interpersonal skills which will complement my professional growth. I feel enjoyment to take challenges to resolve those technical problems which are yet to be solved.
 
My Blog: http://rashimuddin.wordpress.com/
 
My Email: rashimiiuc at yahoo dot com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberNamazi23 Apr '13 - 21:51 
QuestionquestionmemberafroDeluXe31 Oct '12 - 5:57 
GeneralMy vote of 2memberbobfox12 May '12 - 9:51 
QuestionGood JobmemberSaiyed Alam18 Mar '12 - 20:29 
AnswerRe: Good JobmemberMd. Rashim uddin18 Mar '12 - 20:40 
GeneralMy vote of 5memberenamur18 Mar '12 - 20:04 
GeneralRe: My vote of 5memberMd. Rashim uddin18 Mar '12 - 20:41 
Thanks
GeneralMy vote of 4memberJasmin akther Suma17 Mar '12 - 5:53 
GeneralRe: My vote of 4memberMd. Rashim uddin17 Mar '12 - 17:50 

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

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