using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using System.Xml; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; namespace JinYuan.Helper { /// /// 序列化辅助类 /// public class XmlHelper { /// /// 二进制方式的序列化 /// /// 类型 /// 序列化对象 /// 序列化后的内存 public static Stream BinarySerialize(T obj) { MemoryStream serMs = new MemoryStream(); IFormatter iBinaryFormatter = new BinaryFormatter(); iBinaryFormatter.Serialize(serMs, obj); if (serMs.Length > 0) serMs.Position = 0; return serMs; } /// /// 二进制方式的反序列化 /// /// 类型 /// 待序列化的内存 /// 反序列化后的对象 public static T BinaryDeserializer(Stream ms) where T : class { T res = default(T); if (ms.Length > 0) ms.Position = 0; IFormatter iBinaryFormatter = new BinaryFormatter(); res = iBinaryFormatter.Deserialize(ms) as T; return res; } /// /// Xml方式的序列化 /// /// 类型 /// 序列化对象 /// 序列化后的内存 public static Stream XmlSerialize(T obj) { MemoryStream serMs = new MemoryStream(); XmlSerializer ser = new XmlSerializer(obj.GetType()); try { ser.Serialize(serMs, obj); if (serMs.Length > 0) serMs.Position = 0; } catch (Exception ex) { //LogHelper.Current.WriteEx(string.Format("序列化对象异常:{0}", obj.ToString()), ex); return null; } return serMs; } /// /// Xml方式的序列化 /// /// /// public static Stream XmlSerializeObj(object obj) { MemoryStream serMs = new MemoryStream(); XmlSerializer ser = new XmlSerializer(obj.GetType()); ser.Serialize(serMs, obj); if (serMs.Length > 0) serMs.Position = 0; return serMs; } /// /// Xml方式的反序列化 /// /// 类型 /// 待序列化的内存 /// 反序列化后的对象 public static T XmlDeserializer(Stream ms) where T : class { T res = default(T); if (ms.Length > 0) ms.Position = 0; XmlSerializer ser = new XmlSerializer(typeof(T)); res = ser.Deserialize(ms) as T; return res; } /// /// 将List集合保存成XML文档 /// /// /// /// public static bool SaveListToXML(List lstT, string filePath) { try { FileInfo fInfo = new FileInfo(filePath); if (!fInfo.Directory.Exists) { fInfo.Directory.Create(); } Stream stream = XmlSerialize(lstT); if (stream != null) { byte[] bs = StreamIOHelper.ReadStreamBytes(stream); using (FileStream fs = new FileStream(filePath, FileMode.Create)) { fs.Write(bs, 0, bs.Length); } stream.Close(); return true; } else { return false; } } catch (Exception ex) { //LogHelper.Current.WriteEx("将List保存成XML文档发生异常", ex, EnumLogType.Error); return false; } } /// /// 读取XML信息成List集合 /// public static List ReadXMLToList(string filePath) { try { FileInfo fInfo = new FileInfo(filePath); if (!fInfo.Directory.Exists) { fInfo.Directory.Create(); } using (FileStream fs = new FileStream(filePath, FileMode.Open)) { var crList = XmlDeserializer>(fs); return crList; } } catch (Exception ex) { //LogHelper.Current.WriteEx("读取XML转换成List异常", ex, EnumLogType.Error); return null; } } /// /// 将类的实例保存成XML文档 /// /// /// /// /// public static bool SaveModelToXML(T t, string filePath) where T : class { try { FileInfo fInfo = new FileInfo(filePath); if (!fInfo.Directory.Exists) { fInfo.Directory.Create(); } using (FileStream fs = new FileStream(filePath, FileMode.Create)) { using (Stream stream = XmlSerialize(t)) { byte[] bs = StreamIOHelper.ReadStreamBytes(stream); fs.Write(bs, 0, bs.Length); } } return true; } catch (Exception ex) { //LogHelper.Current.WriteEx("将Class保存成XML文档发生异常", ex, EnumLogType.Error); return false; } } /// /// 将XML文档中数据读取到类的实例中 /// /// /// /// public static T ReadXMLToModel(string filePath) where T : class { try { FileInfo fInfo = new FileInfo(filePath); if (!fInfo.Directory.Exists) { fInfo.Directory.Create(); } using (FileStream fs = new FileStream(filePath, FileMode.Open)) { T model = XmlDeserializer(fs); return model; } } catch (Exception ex) { //LogHelper.Current.WriteEx("读取XML转换成实例异常", ex, EnumLogType.Error); return default(T); } } } }