序列化和反序列化
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Json; using System.Text; using System.Web; using System.Xml; using System.Xml.Serialization; namespace wx.Common { public class SerializerHelper { /// <summary> /// XML反序列化 /// </summary> /// <typeparam name="T">转换类型</typeparam> /// <param name="strData">xml数据</param> /// <returns></returns> public static T Deserializer_XML<T>(string strData) { XmlSerializer serializer = new XmlSerializer(typeof(T)); using (StringReader reader = new StringReader(strData)) { return (T)serializer.Deserialize(reader); } } /// <summary> /// XML序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static string Serializer_XML<T>(T obj) { using (StringWriter writer = new StringWriter()) { new XmlSerializer(obj.GetType()).Serialize(writer, obj); return writer.ToString(); } } /// <summary> /// 序列化类实例为XML 并写入.xml文件 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="strPath">文件地址</param> /// <returns></returns> public static void Serializer_XML_Writer<T>(T obj, string strPath) { XmlWriterSettings settings = new XmlWriterSettings(); //设置编码格式 settings.Encoding = Encoding.UTF8; //换行,缩进 settings.Indent = true; using (XmlWriter writer = XmlWriter.Create(strPath, settings)) { //去除默认命名空间xmlns:xsd和xmlns:xsi XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer formatter = new XmlSerializer(typeof(T)); formatter.Serialize(writer, obj, ns); } } /// <summary> /// 序列化类 读取.xml文件实例为Model /// </summary> /// <typeparam name="T">Model</typeparam> /// <param name="obj"></param> /// <param name="strPath">文件地址</param> /// <returns></returns> public static T Deserializer_XML_Reader<T>(string strPath) { using (XmlReader writer = XmlReader.Create(strPath)) { XmlSerializer formatter = new XmlSerializer(typeof(T)); return (T)formatter.Deserialize(writer); } } /// <summary> /// Json反序列化 /// </summary> /// <typeparam name="T">转换类型</typeparam> /// <param name="strJosn">Json数据</param> /// <returns></returns> public static T Deserializer_Json<T>(string strJosn) { //反序列化 using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJosn))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); T obj = (T)serializer.ReadObject(ms); return obj; } } /// <summary> /// Json序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static string Serializer_Json<T>(T obj) { using (MemoryStream ms = new MemoryStream()) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); serializer.WriteObject(ms, obj); return Encoding.UTF8.GetString(ms.ToArray()); } //DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); //MemoryStream ms = new MemoryStream(); //ser.WriteObject(ms, obj); //string jsonString = Encoding.UTF8.GetString(ms.ToArray()); //ms.Close(); //return jsonString; } } }