How To Convert String To XML In C#

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.   
  5.             try  
  6.             {  
  7.                 var xmlserializer = new XmlSerializer(typeof(T));  
  8.                 var stringReader = new StringReader(xml);  
  9.                 using (var reader = XmlReader.Create(stringReader))  
  10.                 {  
  11.                     return (T)xmlserializer.Deserialize(reader);  
  12.                 }  
  13.             }  
  14.             catch (Exception e)  
  15.             {  
  16.                 throw new Exception(e.Message);  
  17.             }  
  18.         } 
Next Recommended Reading Remove Last Character from String in C#