in

Platinum Bay

Peace, Love, Team System, and Community

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 a Team System MVP and INETA Speaker, and has been working as a developer for more than 9 years. During this time, he has designed and developed applications in such widely varying areas as trust accounting, medical information management, supply chain management, and retail systems. Steve is also an MCP, ICSOO, Speaker Liaison for the Philly .NET User Group, and community fanatic.
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.