in

Platinum Bay

Peace, Love, and Visual Studio ALM

This Blog

Syndication


.NETicated

Serialization Generics

I really dislike writing the same code over and over again. For my current project, this involves serialization and I ended up creating the following generic class to handle Xml serialization and thought it might be useful to someone else.

using System.Xml.Serialization;

public class SerializationHelper<T> where T : class
{
    public string SerializeXml(T o)
    {
        var serializer = new XmlSerializer(typeof(T));

        var sb = new StringBuilder();
        using (var tw = new StringWriter(sb))
        {
            serializer.Serialize(tw, o);
        }

        return sb.ToString();
    }

    public T DeserializeXml(string text)
    {
        var serializer = new XmlSerializer(typeof(T));

        using (var tr = new StringReader(text))
        {
            return (T)serializer.Deserialize(tr);
        }
    }
}
Published Dec 07 2009, 07:05 AM by Steve
Filed under: ,

Comments

December 7, 2009 6:02 AM

Pingback from  Dew Drop &#8211; December 7, 2009 | Alvin Ashcraft&#039;s Morning Dew

 

December 7, 2009 9:22 AM

Daily tech links for .net and related technologies - December 7-8, 2009 Web Development Authenticated

 

December 7, 2009 9:44 AM

Pingback from  Dew Drop &#8211; Experimental Edition &#8211; December 7, 2009 | Alvin Ashcraft&#039;s Morning Dew

 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is an independent consultant, INETA speaker, and Microsoft MVP for Visual Studio ALM. He has been working in technology for over ten years focusing on custom application development and Application Lifecycle Management. Steve is also Microsoft and IBM certified and a community fanatic having led sessions at nearly 100 events across North America. When he's not developing software solutions or engaging with the community about software technology, Steve is a closet singer and songwriter and plays the guitar and keys. Occasionally, Steve even gets to sleep. Occasionally.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.