C#中XML与对象之间的序列化、反序列化

2021-05-13 11:29

阅读:341

标签:stream   exce   c#   blog   string   一个   void   toe   tty   

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

namespace Xml.Utility
{
    public static class XmlUtil
    {
        /// 
        /// 将一个对象序列化为XML字符串
        /// 
        /// 要序列化的对象
        /// 编码方式
        /// 序列化产生的XML字符串
        public static string XmlSerialize(object o, Encoding encoding)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }

            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
          
            using (MemoryStream stream = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(o.GetType());

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineChars = "\r\n";
                settings.Encoding = encoding;
                settings.IndentChars = "  ";
                settings.OmitXmlDeclaration = true;

                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    //Create our own namespaces for the output
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    //Add an empty namespace and empty value
                    ns.Add("", "");
                    serializer.Serialize(writer, o, ns);
                    writer.Close();
                }

                stream.Position = 0;
                using (StreamReader reader = new StreamReader(stream, encoding))
                {
                    return reader.ReadToEnd();
                }
            }
        }

        /// 
        /// 从XML字符串中反序列化对象
        /// 
        /// 结果对象类型
        /// 包含对象的XML字符串
        /// 编码方式
        /// 反序列化得到的对象
        public static T XmlDeserialize(string s, Encoding encoding)
        {
            if (string.IsNullOrEmpty(s))
                throw new ArgumentNullException("s");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            XmlSerializer mySerializer = new XmlSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s)))
            {
                using (StreamReader sr = new StreamReader(ms, encoding))
                {
                    return (T)mySerializer.Deserialize(sr);
                }
            }
        }

        /// 
        /// 将一个对象按XML序列化的方式写入到一个文件
        /// 
        /// 要序列化的对象
        /// 保存文件路径
        /// 编码方式
        public static void XmlSerializeToFile(object o, string path, Encoding encoding)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (o == null)
            {
                throw new ArgumentNullException("o");
            }

            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                XmlSerializer serializer = new XmlSerializer(o.GetType());

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineChars = "\r\n";
                settings.Encoding = encoding;
                settings.IndentChars = "    ";

                using (XmlWriter writer = XmlWriter.Create(file, settings))
                {
                    serializer.Serialize(writer, o);
                    writer.Close();
                }
            }
        }

        /// 
        /// 读入一个文件,并按XML的方式反序列化对象。
        /// 
        /// 结果对象类型
        /// 文件路径
        /// 编码方式
        /// 反序列化得到的对象
        public static T XmlDeserializeFromFile(string path, Encoding encoding)
        {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            string xml = File.ReadAllText(path, encoding);
            return XmlDeserialize(xml, encoding);
        }
    }
}

 

C#中XML与对象之间的序列化、反序列化

标签:stream   exce   c#   blog   string   一个   void   toe   tty   

原文地址:http://www.cnblogs.com/zhangchenliang/p/7552303.html


评论


亲,登录后才可以留言!