Last XML serializer I’ll ever write

I’ve made this class probably a half dozen times, and I’m getting pretty tired of writing it.  It seems like every application I write has to serialize and deserialize back and forth between XML strings and objects.  For future reference, here it is:

public static class XmlSerializationHelper
{
    public static string Serialize<T>(T value)
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        StringWriter writer = new StringWriter();
        xmlSerializer.Serialize(writer, value);

        return writer.ToString();
    }

    public static T Deserialize<T>(string rawValue)
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        StringReader reader = new StringReader(rawValue);

        T value = (T)xmlSerializer.Deserialize(reader);
        return value;
    }
}

I like this implementation as it’s generic so it removes some casting from the user.  Yeah, I know this code is probably in a hundred other blogs.  But at least now I know exactly where to find it.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in C#. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.mostlyclean.com Steve Burman

    lol – I think I have written this class at least a half-dozen times myself.

    I tend to make mine a single SerializationHelper that can do both Xml and Binary serialization. So I have the following methods as well:

    public static byte[] BinarySerialize(object obj)
    {
    using (MemoryStream ms = new MemoryStream())
    {
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(ms, obj);
    return ms.ToArray();
    }
    }

    public static T BinaryDeserialize(byte[] data) where T : class
    {
    using (MemoryStream ms = new MemoryStream(data))
    {
    BinaryFormatter formatter = new BinaryFormatter();
    return formatter.Deserialize(ms) as T;
    }
    }

    (apologies in advance – as I’m sure the formatting of that code sux as a comment)

  • http://www.codesqueeze.com Max Pool

    Well done. Not a fan of code snippets, but this is one I might put in my pocket…

  • http://jimmybogard.lostechies.com Jimmy Bogard

    @Steve

    Yeah thanks for the heads up on the binary serialization, I always forget about that one too.

    @Max

    Neither am I normally, but I’m subverting my blog as a personal code repository on this one :)

  • http://www.flux88.com Ben Scheirman

    You could also use this in an object extension method…

    someObject.ToXml()

    and SomeObjectClass.FromXml();

    (you’d have to extend your helper to allow for Type parameters b/c generic calls without knowing the T at compile time is only doable through ugly reflection)

  • http://jimmybogard.lostechies.com Jimmy Bogard

    Ah Ben, but that’s where you’re wrong! A challenge!

    public static string ToXml(this T value)
    {
    return Serialize(value);
    }

    public static T FromXml(this string rawValue)
    {
    return Deserialize
    (rawValue);
    }

    Generic extension methods! Success!

  • XStream

    use XStream…

  • Garth

    Thank you I used this in my code. :)