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();
        }
    }
}
相关推荐
孞㐑¥5 分钟前
C++11介绍
开发语言·c++·经验分享·笔记
旦莫13 分钟前
Python 教程:我们可以给 Python 文件起中文名吗?
开发语言·python
剁椒排骨39 分钟前
win11什么都不动之后一段时间黑屏桌面无法显示,但鼠标仍可移动,得要熄屏之后才能进入的四种解决方法
运维·windows·经验分享·计算机外设·win11·win10
꧁坚持很酷꧂1 小时前
配置Ubuntu18.04中的Qt Creator为中文(图文详解)
开发语言·qt·ubuntu
李菠菜1 小时前
Windows Terminal 集成 Git Bash 的简洁配置指南
windows·git
不当菜虚困1 小时前
JAVA设计模式——(四)门面模式
java·开发语言·设计模式
ruyingcai6666661 小时前
用python进行OCR识别
开发语言·python·ocr
m0Java门徒1 小时前
面向对象编程核心:封装、继承、多态与 static 关键字深度解析
java·运维·开发语言·intellij-idea·idea
liuweidong08021 小时前
【Pandas】pandas DataFrame radd
开发语言·python·pandas
农民也会写代码2 小时前
dedecms织梦arclist标签noflag属性过滤多个参数
开发语言·数据库·sql·php·dedecms