#region << 版 本 注 释 >> /*---------------------------------------------------------------- * 创建者:Hupe * 创建时间:2021/11/15 14:40:30 * 版本:V1.0.0 * 描述: * * ---------------------------------------------------------------- * 修改人: * 时间: * 修改说明: * * 版本:V1.0.1 *----------------------------------------------------------------*/ #endregion << 版 本 注 释 >> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using System.IO; using System.Xml; using ARI.EAP.HOST.Common; using System.Windows.Forms; using Glorysoft.SECS.EQP.Utilities; namespace ARI.EAP.HOST.Utilities { /// /// Xml序列化与反序列化 /// Hupe /// public class XmlSerializeUtil { #region 反序列化 /// /// 反序列化 /// /// XML字符串 /// public static T Deserialize(string xml) { try { using (StringReader sr = new StringReader(xml)) { XmlSerializer xmldes = new XmlSerializer(typeof(T)); return (T)xmldes.Deserialize(sr); } } catch(Exception e) { MessageBox.Show(e.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); LoggerService.SYSLogger.Error(e); return default(T); } } #endregion #region 序列化为xml文件并保存到本地 /// /// 序列化 /// /// 对象 /// public static void Serializer(T obj) { try { using (StreamWriter sw = new StreamWriter(Constants.configuerPath, false, new UTF8Encoding(false))) { Type t = obj.GetType(); XmlSerializer xml = new XmlSerializer(t); //序列化对象 xml.Serialize(sw, obj); sw.Close(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Save(sw); } } catch(Exception e) { MessageBox.Show(e.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); LoggerService.SYSLogger.Error(e); } } #endregion } }