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