using System.Xml.Serialization; using Newtonsoft.Json;
xml转换
/// <summary> /// XML转换实体类 /// </summary> /// <typeparam name="T">xml对应的实体类</typeparam> /// <param name="res">xml字符串</param> /// <returns>失败返回null,成功返回对应T</returns> public T GetXmlT<T>(string res) where T : class { try { XmlSerializer serializer = new XmlSerializer(typeof(T)); StringReader rdr = new StringReader(res); T rt = serializer.Deserialize(rdr) as T; return rt; } catch(Exception ex){ return null; } }
JSON转换
/// <summary> /// json转换得到实体对象 /// </summary> /// <typeparam name="T">json对应的实体对象</typeparam> /// <param name="res">json字符串</param> /// <returns>失败返回null,成功返回对应T</returns> public T GetJsonT<T>(string res) where T : class { try { return JsonConvert.DeserializeObject<T>(res); } catch (Exception ex) { return null; } }
时间: 2024-10-15 06:01:58