最近因為API需要解析xml,所以整理了一下之前用到反序列化小工具。
public class XmlHelper { public static TSource TryPareseXml<TSource>(string xmlString) { XmlSerializer serializer = new XmlSerializer(typeof(TSource)); TSource result; //反序列化 using (TextReader reader = new StringReader(xmlString)) { try { result = (TSource)serializer.Deserialize(reader); } catch (Exception) { return default(TSource); ; } return result; } } }