此文记录的是XML文件的序列化和反序列化工具类。
/***XML工具类Austin Liu 刘恒辉Project Manager and Software DesignerE-Mail: lzhdim@163.comBlog: http://lzhdim.cnblogs.comDate: 2024-01-15 15:18:00使用说明:/// <summary>/// 加载程序函数/// </summary>/// <param name="appConfigFilePath"></param>/// <returns></returns>internal static AppConfigModule LoadSetting(string appConfigFilePath){AppConfigModule list;try{list = (AppConfigModule)XMLUtil.Deserialize(new AppConfigModule(), FileUtil.ReadFile(appConfigFilePath));}catch{return null;}return list;}/// <summary>/// 保存程序函数/// </summary>/// <param name="ListProgram"></param>/// <param name="appConfigFilePath"></param>internal static void SaveSetting(AppConfigModule ListProgram, string appConfigFilePath){FileUtil.SaveFile(appConfigFilePath, XMLUtil.Serialize(ListProgram));}***/namespace Lzhdim.LPF.Utility
{using System;using System.IO;using System.Xml.Serialization;/// <summary>/// The Object End of XML/// </summary>public static class XMLUtil{/// <summary>/// Deserialize string to an object/// </summary>/// <param name="obj">the object which need to Deserialize</param>/// <param name="serializedString">string which need to Deserialize</param>/// <returns>Object</returns>public static Object Deserialize(Object obj, string serializedString){XmlSerializer xsw = new XmlSerializer(obj.GetType());TextReader s = new StringReader(serializedString);return xsw.Deserialize(s);}/// <summary>/// Deserialize string to an object/// </summary>/// <param name="objectType">the Type of object which need to Deserialize</param>/// <param name="serializedString">string which need to Deserialize</param>/// <returns>Object</returns>public static Object Deserialize(Type objectType, string serializedString){XmlSerializer xsw = new XmlSerializer(objectType);TextReader s = new StringReader(serializedString);return xsw.Deserialize(s);}/// <summary>/// Deserialize string to an object/// </summary>/// <param name="serializedString">string which need to Deserialize</param>/// <param name="listType">the Type of object which need to Deserialize</param>/// <param name="elementType">the element type of the objectlist</param>/// <returns>object</returns>public static Object Deserialize(string serializedString, Type listType, Type elementType){XmlSerializer serializer = new XmlSerializer(listType, new Type[] { elementType });StringReader textReader = new StringReader(serializedString);return serializer.Deserialize(textReader);}/// <summary>/// Serialize an object to string/// </summary>/// <param name="obj">object which need to Serialize</param>/// <returns>string</returns>public static string Serialize(Object obj){XmlSerializer serializer = new XmlSerializer(obj.GetType());StringWriter writer = new StringWriter();serializer.Serialize((TextWriter)writer, obj);return writer.ToString();}/// <summary>/// Serialize an object to string/// </summary>/// <param name="objList">object which need to Serialize</param>/// <param name="elementType">the element type of the objectlist</param>/// <returns></returns>public static string Serialize(object objList, Type elementType){XmlSerializer serializer = new XmlSerializer(objList.GetType(), new Type[] { elementType });StringWriter writer = new StringWriter();serializer.Serialize((TextWriter)writer, objList);return writer.ToString();}}
}