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();
        }
    }
}
相关推荐
我不会编程55515 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
李少兄15 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
“抚琴”的人15 小时前
【机械视觉】C#+VisionPro联合编程———【六、visionPro连接工业相机设备】
c#·工业相机·visionpro·机械视觉
无名之逆15 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
似水এ᭄往昔15 小时前
【C语言】文件操作
c语言·开发语言
啊喜拔牙16 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala
xixixin_16 小时前
为什么 js 对象中引用本地图片需要写 require 或 import
开发语言·前端·javascript
W_chuanqi16 小时前
安装 Microsoft Visual C++ Build Tools
开发语言·c++·microsoft
anlogic16 小时前
Java基础 4.3
java·开发语言
A旧城以西17 小时前
数据结构(JAVA)单向,双向链表
java·开发语言·数据结构·学习·链表·intellij-idea·idea