dc/Utilities/XmlSerializeUtil.cs
13118993771@163.com 5e9d0f1e2d EAP
2022-04-01 17:03:54 +08:00

91 regels
2.7 KiB
C#
Ruw Permalink Blame Geschiedenis

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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
{
/// <summary>
/// <remarks>Xml序列化与反序列化</remarks>
/// <creator>Hupe</creator>
/// </summary>
public class XmlSerializeUtil
{
#region
/// <summary>
/// 反序列化
/// </summary>
/// <param name="xml">XML字符串</param>
/// <returns></returns>
public static T Deserialize<T>(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文件并保存到本地
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <returns></returns>
public static void Serializer<T>(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
}
}