To achieve this, we can use the XmlWriter class provided by the following namespaces: "System.Xml", "System.Xml.Serialization" and "System.IO". Here is a custom code snippet, where the first parameter is the string and return type of method is generic T.
  1. public static T Deserialize<T>(string xml)
  2. {
  3. if (String.IsNullOrEmpty(xml)) throw new NotSupportedException("Empty string!!");
  4. try
  5. {
  6. var xmlserializer = new XmlSerializer(typeof(T));
  7. var stringReader = new StringReader(xml);
  8. using (var reader = XmlReader.Create(stringReader))
  9. {
  10. return (T)xmlserializer.Deserialize(reader);
  11. }
  12. }
  13. catch (Exception e)
  14. {
  15. throw new Exception(e.Message);
  16. }
  17. }
Related tutorial