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