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

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

复制代码
/***

    XML工具类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: [email protected]
    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();
        }
    }
}
相关推荐
ascarl201011 分钟前
准确--k8s cgroup问题排查
java·开发语言
fpcc1 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
莱茵菜苗1 小时前
Python打卡训练营day46——2025.06.06
开发语言·python
爱学习的小道长1 小时前
Python 构建法律DeepSeek RAG
开发语言·python
luojiaao2 小时前
【Python工具开发】k3q_arxml 简单但是非常好用的arxml编辑器,可以称为arxml杀手包
开发语言·python·编辑器
终焉代码2 小时前
STL解析——list的使用
开发语言·c++
SoFlu软件机器人2 小时前
智能生成完整 Java 后端架构,告别手动编写 ControllerServiceDao
java·开发语言·架构
英英_2 小时前
视频爬虫的Python库
开发语言·python·音视频
猛犸MAMMOTH2 小时前
Python打卡第46天
开发语言·python·机器学习
多多*2 小时前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap