欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#实现XML文件的序列化和反序列化

程序员文章站 2022-03-04 14:24:57
...

一、C#实现XML文件的序列化和反序列化核心

/***
*	Title:"基础工具" 项目
*		主题:XML帮助类
*	Description:
*		功能:
*		    1、将对象序列化为xml文件且保存
*		    2、将对象序列化为xml字符串
*		    3、将xml文件反序列化为对象
*		    4、将xml字符串反序列化为对象
*	Date:2021
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Utils
{
    class XmlHelper
    {

        /// <summary>
        /// 将对象序列化为xml文件且保存
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="t">对象</param>
        /// <param name="filePathAndName">xml存放路径和名称</param>
        /// <returns>True:表示成功</returns>
        public static bool ObjectToXmlFile<T>(T t, string filePathAndName) where T : class
        {
            if (t == null || string.IsNullOrEmpty(filePathAndName)) return false;

            bool success = false;
            try
            {
                XmlSerializer formatter = new XmlSerializer(typeof(T));
                using (FileStream stream = new FileStream(filePathAndName, FileMode.OpenOrCreate))
                {
                    formatter.Serialize(stream, t);
                    success = true;
                }
            }
            catch (Exception ex)
            {
                success = false;
                throw new Exception(ex.Message);
            }
            return success;
        }

        /// <summary>
        /// 将对象序列化为xml字符串
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="t">对象</param>
        public static string ObjectToXmlStr<T>(T t) where T : class
        {
            if (t == null) return null;

            try
            {
                XmlSerializer formatter = new XmlSerializer(typeof(T));
                using (MemoryStream stream = new MemoryStream())
                {
                    formatter.Serialize(stream, t);
                    string result = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                    return result;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        /// <summary>
        /// 将xml文件反序列化为对象
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="t">对象</param>
        /// <param name="filePathAndName">xml文件的路径和名称</param>
        /// <returns>对象</returns>
        public static T XmlFileToObject<T>(string filePathAndName) where T : class
        {
            if (string.IsNullOrEmpty(filePathAndName)) return null;

            try
            {
                XmlSerializer formatter = new XmlSerializer(typeof(T));
                using (FileStream stream = new FileStream(filePathAndName, FileMode.OpenOrCreate))
                {
                    XmlReader xmlReader = new XmlTextReader(stream);
                    T result = formatter.Deserialize(xmlReader) as T;
                    return result;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        /// <summary>
        /// 将xml字符串反序列化为对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="xmlStr">xml文件字符串</param>
        /// <returns></returns>
        public static T XmlStrToObject<T>(string xmlStr) where T : class
        {
            if (string.IsNullOrEmpty(xmlStr)) return null;

            try
            {
                using (StringReader sr = new StringReader(xmlStr))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    return serializer.Deserialize(sr) as T;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }


    }//Class_end

}

二、使用方法

①引用命名空间

using Utils;

②调用方法即可使用,比如实现将对象(UpdatePackageInfo)序列化为字符串且保存

serverUpdateInfo = XmlHelper.XmlFileToObject<UpdatePackageInfo>(@"C:\software\Document\updatePackage.xml");

③示例中的UpdatePackageInfo对象

/***
*	Title:"数据采集" 项目
*		主题:升级包的信息对象
*	Description:
*		功能:XXX
*	Date:2021
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Model
{
    [XmlRoot]
    public class UpdatePackageInfo
    {
        /// <summary>
        /// 版本号
        /// </summary>
        [XmlElement]
        public string Version { get; set; }

        /// <summary>
        /// 更新包文件地址
        /// </summary>
        [XmlElement]
        public string UpdatePackageFileAddress { get; set; }

        /// <summary>
        /// 更新包配置地址
        /// </summary>
        [XmlElement]
        public string UpdatePackageConfigAddress { get; set; }

        /// <summary>
        /// 文件名称
        /// </summary>
        [XmlElement]
        public string FileName { get; set; }

        /// <summary>
        /// 文件的Hash值
        /// </summary>
        [XmlElement]
        public string FileHash { get; set; }


    }//Class_end

}