Using generics with XmlSerializer

An addition to @Oded, you can make the method Generic aswell:

public T ConvertXml<T>(string xml)
{
    var serializer = new XmlSerializer(typeof(T));
    return (T)serializer.Deserialize(new StringReader(xml));
}

This way you don’t need to make the whole class generic and you can use it like this:

var result = ConvertXml<MyObject>(source);

Leave a Comment