Tuesday, November 4, 2014

a static C# method for deserializing XML to a specific type

public static T Deserialize<T>(string xml)
{
   var xmlReader = XmlReader.Create(new StringReader(xml), new
         XmlReaderSettings());
   var xmlSerializer = new XmlSerializer(typeof(T));
   var deserializedObject = xmlSerializer.Deserialize(xmlReader);
   return (T)deserializedObject;
}

 
 

Use it like so:

var foo = Whatever.Deserialize<Foo>(myXml)

No comments:

Post a Comment