123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using System.IO;
- using System.Xml.Serialization;
- namespace PaintDotNet.Base.CommTool
- {
- /// <summary>
- /// XML序列化公共处理类
- /// </summary>
- public static class XmlSerializeHelper
- {
- /// <summary>
- /// 将实体对象转换成XML
- /// </summary>
- /// <typeparam name="T">实体类型</typeparam>
- /// <param name="obj">实体对象</param>
- public static string XmlSerialize<T>(T obj)
- {
- try
- {
- using (StringWriter sw = new StringWriter())
- {
- Type t = obj.GetType();
- XmlSerializer serializer = new XmlSerializer(obj.GetType());
- serializer.Serialize(sw, obj);
- sw.Close();
- return sw.ToString();
- }
- }
- catch (Exception ex)
- {
- throw new Exception("", ex); // ("将实体对象转换成XML异常", ex); //("", exception); //
- }
- }
- /// <summary>
- /// 将XML转换成实体对象
- /// </summary>
- /// <typeparam name="T">实体类型</typeparam>
- /// <param name="strXML">XML</param>
- public static T DESerializer<T>(string strXML) where T : class
- {
- try
- {
- using (StringReader sr = new StringReader(strXML))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(T));
- return serializer.Deserialize(sr) as T;
- }
- }
- catch (Exception ex)
- {
- throw new Exception("", ex); // ("将XML转换成实体对象异常", ex);
- }
- }
- /// <summary>
- /// 将实体对象转换成XML
- /// </summary>
- /// <typeparam name="T">实体类型</typeparam>
- /// <param name="obj">实体对象</param>
- public static void Save<T>(T obj, string path)
- {
- try
- {
- using (var wirter = new FileStream(path, (FileMode)FileAccess.Write))
- {
- Type t = obj.GetType();
- XmlSerializer serializer = new XmlSerializer(obj.GetType());
- serializer.Serialize(wirter, obj);
- wirter.Close();
- }
- }
- catch (Exception ex)
- {
- throw new Exception("", ex);
- }
- }
- /// <summary>
- /// 将XML转换成实体对象
- /// </summary>
- /// <typeparam name="T">实体类型</typeparam>
- /// <param name="strXML">XML</param>
- public static T Load<T>(string path) where T : class
- {
- try
- {
- using (var reader = new FileStream(path, FileMode.Open))
- {
- XmlSerializer xml = new XmlSerializer(typeof(T));
- var obj = xml.Deserialize(reader);
- reader.Close();
- return obj as T;
- }
- }
- catch (Exception ex)
- {
- throw new Exception("", ex);
- }
- }
- }
- }
|