12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Serialization;
- using SenseShield;
- namespace SmartCoalApplication.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); //
- }
- }
- static bool IS_INIT = false;
- /// <summary>
- /// 将XML转换成实体对象
- /// </summary>
- /// <typeparam name="T">实体类型</typeparam>
- /// <param name="strXML">XML</param>
- public static T DESerializer<T>(string strXML) where T : class
- {
- try
- {
- //if (!IS_INIT)
- //{
- // //许可ID是 10000 或者 4 都可以
- // DogDecrypting.decrypting(new uint[] { 10000, 4, 41, 42, 3,5 });
- // IS_INIT = true;
- //}
- 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);
- }
- }
- }
- }
|