XML工具类 - C#小函数类推荐

此文记录的是XML文件的序列化和反序列化工具类。

/***

    XML工具类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   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();
        }
    }
}
相关推荐
橘子海全栈攻城狮28 分钟前
【源码+文档+调试讲解】“健康早知道”微信小程序
开发语言·servlet·微信小程序·小程序·notepad++
Camllia3731 分钟前
游戏无法运行、图形显示错误、视频播放卡顿、音频播放无声等等电脑问题要怎么轻松解决?使用修复工具推荐!
windows·经验分享·游戏·电脑
爱是小小的癌37 分钟前
Java-数据结构-顺序表(ArrayList)
java·开发语言·数据结构
我明天再来学Web渗透1 小时前
【2024年-11月-9日-开源社区openEuler实践记录】OpenAMDC:开启智能边缘计算与系统管控的新征程
开发语言·人工智能·架构·开源·边缘计算·copilot·开源软件
egoist20231 小时前
数据结构之单链表(超详解)
c语言·开发语言·数据结构·笔记·学习·链表·gitee
高思宇1 小时前
Oracle删除归档日志
数据库·windows·oracle
小_太_阳1 小时前
Scala_【3】运算符
开发语言·scala·intellij-idea
xinghuitunan1 小时前
考试座位号(PTA)C语言
c语言·开发语言
前网易架构师-高司机1 小时前
游泳溺水识别数据集,对25729张图片进行YOLO,COCO JSON, VOC XML 格式的标注,溺水平均识别率在89.9%
xml·yolo
程序边界1 小时前
AIGC赋能Java编程:智能工具引领效率、创新与理解的新纪元
java·开发语言·aigc