C#编程,XML转换工具类

自己写的一个工具类,用于XML的转换。

包括:Object对象转XML文件,

          Object对象转XML字符串,

          XML格式的文件转Object对象,

          XML字符串转Object对象。

使用C#语言,VS2013开发工具, .NET4.5 。


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

namespace XmlDemo
{
    /// <summary>
    /// XML转换帮助类
    /// </summary>
    public class XmlHelper
    {
        /// <summary>
        /// 将一个对象序列化为XML字符串。
        ///
        /// 参数:
        /// obj,object对象;
        /// encoding,编码方式,一般用UTF-8
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string ObjectToXMLStr(object obj, Encoding encoding)
        {
            string str = null;

            MemoryStream stream = null;
            StreamReader reader = null;
            XmlWriter writer = null;

            try
            {
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineChars = "\r\n";
                settings.Encoding = encoding;
                settings.IndentChars = "\t";

                stream = new MemoryStream();
                writer = XmlWriter.Create(stream, settings);
                serializer.Serialize(writer, obj);
                stream.Position = 0;
                reader = new StreamReader(stream, encoding);
                str = reader.ReadToEnd();
            }
            catch (Exception)
            {
                str = null;
                throw;
            }
            finally
            {
                writer.Close();
                reader.Close();
                stream.Close();
            }

            return str;
        }



        /// <summary>
        /// 将一个对象按XML序列化的方式写入到一个文件(推荐用XML文件)。
        /// 
        /// 参数:
        /// obj,object对象;
        /// path,文件绝对路径;
        /// encoding,编码方式,一般用UTF-8。
        /// 返回值:
        /// bool类型,true表示操作成功,false表示失败
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="path"></param>
        /// <param name="encoding"></param>
        public static bool ObjectToXMLFile(object obj, string path, Encoding encoding)
        {
            bool b = false;

            if (obj == null)
            {
                throw new ArgumentNullException("参数obj不能为null,请传入有效的对象");
            }
            if (path == null)
            {
                throw new ArgumentNullException("参数path(文件路径)不能为null");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("参数encoding不能为null,请确定有效的字符编码方式。");
            }

            FileStream fs = null;
            XmlWriter writer = null;
            try
            {
                fs = new FileStream(path, FileMode.Create, FileAccess.Write);

                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineChars = "\r\n";
                settings.Encoding = encoding;
                settings.IndentChars = "\t";

                writer = XmlWriter.Create(fs, settings);

                serializer.Serialize(writer, obj);

                b = true;
            }
            catch (Exception)
            {
                b = false;
                throw;
            }
            finally
            {
                writer.Close();
                fs.Close();
            }

            return b;
        }

       

        /// <summary>
        /// 读取一个文件(推荐用XML文件),并按XML的方式反序列化对象。
        /// 
        /// 参数:
        /// path,文件绝对路径;
        /// encoding,编码方式,一般用UTF-8。
        /// 返回值:
        /// object对象
        /// </summary>
        /// <param name="path"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static T XMLFlieToObject<T>(string path, Encoding encoding)
        {
            T T_object = default(T);

            if (path == null || !File.Exists(path))
            {
                throw new ArgumentNullException("系统找不到你指定的文件。");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("参数encoding不能为null,请确定有效的字符编码方式。");
            }

            try
            {
                string xml = File.ReadAllText(path, encoding);
                T_object = XMLStrToObject<T>(xml);
            }
            catch (Exception)
            {
                throw;
            }

            return T_object;
        }



        /// <summary>
        /// 从XML字符串中反序列化对象。
        ///
        /// 参数:
        /// str,XML字符串;
        /// encoding,编码方式,一般用UTF-8。
        /// 返回值:
        /// object对象
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T XMLStrToObject<T>(string str)
        {
            T T_object = default(T);

            if (str == null || str == "")
            {
                throw new ArgumentNullException("参数str不能为空值,请传入有效的XML字符串。");
            }

            TextReader reader = null;
            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                reader = new StringReader(str);
                T_object = (T)xmlSerializer.Deserialize(reader);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                reader.Close();
            }

            return T_object;
        }


    }
}



自己在编程时觉得封装起来的工具类很好用,所以分享给大家,希望给你带来帮助!

若其中有瑕疵,还望指教!





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值