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();
        }
    }
}
相关推荐
honghongstand36 分钟前
代码随想录D52-53 图论 Python
开发语言·python·图论
过客猫202242 分钟前
使用 deepseek实现 go语言,读取文本文件的功能,要求支持 ascii,utf-8 等多种格式自适应
开发语言·后端·golang
程序媛-徐师姐1 小时前
基于 Python Django 的校园互助平台(附源码,文档)
开发语言·python·django·校园互助·校园互助平台
且听风吟ayan1 小时前
leetcode day20 滑动窗口209+904
算法·leetcode·c#
a小胡哦1 小时前
Windows、Mac、Linux,到底该怎么选?
linux·windows·macos·操作系统
进击的_鹏1 小时前
【C++】list 链表的使用+模拟实现
开发语言·c++·链表
m0_738355691 小时前
java泛型
java·开发语言
大模型铲屎官2 小时前
哈希表入门到精通:从原理到 Python 实现全解析
开发语言·数据结构·python·算法·哈希算法·哈希表
L_09072 小时前
【C】队列与栈的相互转换
c语言·开发语言·数据结构
qq4054251972 小时前
基于python的旅客游记和轨迹分析可视化系统设计(新)
开发语言·python