using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml.Serialization; using System.Data; using System.Runtime.Serialization.Formatters.Soap; using System.Runtime.Serialization.Formatters.Binary; namespace Mesnac.Basic { /// /// 序列化和反序列化处理类 /// public class SerializeHandler { #region 序列化和反序列化 /// /// 序列化指定类型的对象到指定的文件 /// /// 要序列化的对象类型 /// 要序列化的对象 /// 保存对象数据的完整文件名 public static void Serialize(T obj, string fileName) { lock (fileName) { try { string dir = Path.GetDirectoryName(fileName); //获取文件路径 if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } BinaryFormatter formatter = new BinaryFormatter(); FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); formatter.Serialize(fs, obj); fs.Close(); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(ex.Message); ICSharpCode.Core.LoggingService.Error(ex.StackTrace); } } } /// /// 从指定的文件中反序列化指定类型的对象 /// /// 反序列化的对象类型 /// 保存对象数据的文件名 /// 返回反序列化出的对象实例 public static T Deserialize(string fileName) { lock (fileName) { try { if (!File.Exists(fileName)) { ICSharpCode.Core.LoggingService.Debug(String.Format("序列化文件{0}不存在!", fileName)); return default(T); } else { BinaryFormatter formatter = new BinaryFormatter(); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); T obj = (T)formatter.Deserialize(fs); fs.Close(); return obj; } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(ex.Message); ICSharpCode.Core.LoggingService.Error(ex.StackTrace); return default(T); } } } /// /// 序列化指定类型的对象到指定的Xml文件 /// /// 要序列化的对象类型 /// 要序列化的对象 /// 保存对象数据的完整文件名 public static void SerializeXml(T obj, string fileName) { lock (fileName) { try { string dir = Path.GetDirectoryName(fileName); //获取文件路径 if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } SoapFormatter formatter = new SoapFormatter(); FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); formatter.Serialize(fs, obj); fs.Close(); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(ex.Message); ICSharpCode.Core.LoggingService.Error(ex.StackTrace); } } } /// /// 从指定的Xml文件中反序列化指定类型的对象 /// /// 反序列化的对象类型 /// 保存对象数据的文件名 /// 返回反序列化出的对象实例 public static T DeserializeXml(string fileName) { lock (fileName) { try { if (!File.Exists(fileName)) { ICSharpCode.Core.LoggingService.Debug(String.Format("序列化文件{0}不存在!", fileName)); return default(T); } else { SoapFormatter formatter = new SoapFormatter(); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); T obj = (T)formatter.Deserialize(fs); fs.Close(); return obj; } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(ex.Message); ICSharpCode.Core.LoggingService.Error(ex.StackTrace); return default(T); } } } /// /// 把DataTable对象序列化为XML文本字符串 /// /// 要序列化的DataTable对象 /// 返回序列化后的XML文本字符串 public static string SerializeDataTable(DataTable dt) { StringWriter strWriter = new StringWriter(); XmlSerializer serializer = new XmlSerializer(typeof(DataTable)); serializer.Serialize(strWriter, dt); return strWriter.ToString(); } /// /// 把对象序列化为xml字符串 /// /// /// /// public static string SerializeObject(T obj) { if (obj != null) { StringWriter strWriter = new StringWriter(); XmlSerializer serializer = new XmlSerializer(typeof(T)); serializer.Serialize(strWriter, obj); return strWriter.ToString(); } else { return String.Empty; } } /// /// 把xml字符串反序列化为对象 /// /// /// /// public static T DeserializeObject(string xmlString) { if (!String.IsNullOrEmpty(xmlString)) { StringReader strReader = new StringReader(xmlString); XmlSerializer serializer = new XmlSerializer(typeof(T)); T obj = (T)serializer.Deserialize(strReader); return obj; } else { return default(T); } } #endregion } }