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


    直接上代码:

    using System;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace Xml.Utility
    {
        public static class XmlUtil
        {
            /// <summary>
            /// 将一个对象序列化为XML字符串
            /// </summary>
            /// <param name="o">要序列化的对象</param>
            /// <param name="encoding">编码方式</param>
            /// <returns>序列化产生的XML字符串</returns>
            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 = "
    ";
                    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();
                    }
                }
            }
    
            /// <summary>
            /// 从XML字符串中反序列化对象
            /// </summary>
            /// <typeparam name="T">结果对象类型</typeparam>
            /// <param name="s">包含对象的XML字符串</param>
            /// <param name="encoding">编码方式</param>
            /// <returns>反序列化得到的对象</returns>
            public static T XmlDeserialize<T>(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);
                    }
                }
            }
    
            /// <summary>
            /// 将一个对象按XML序列化的方式写入到一个文件
            /// </summary>
            /// <param name="o">要序列化的对象</param>
            /// <param name="path">保存文件路径</param>
            /// <param name="encoding">编码方式</param>
            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 = "
    ";
                    settings.Encoding = encoding;
                    settings.IndentChars = "    ";
    
                    using (XmlWriter writer = XmlWriter.Create(file, settings))
                    {
                        serializer.Serialize(writer, o);
                        writer.Close();
                    }
                }
            }
    
            /// <summary>
            /// 读入一个文件,并按XML的方式反序列化对象。
            /// </summary>
            /// <typeparam name="T">结果对象类型</typeparam>
            /// <param name="path">文件路径</param>
            /// <param name="encoding">编码方式</param>
            /// <returns>反序列化得到的对象</returns>
            public static T XmlDeserializeFromFile<T>(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<T>(xml, encoding);
            }
        }
    }
    View Code
  • 相关阅读:
    Servlet----------在 Servlet 中的xml配置
    java连接数据库时的报错
    借用HTML5 插入视频。音频
    多线程的总结
    穷举法例子
    利用递归求最大公约数和最小公倍数
    递归逆序的使用
    Mac OS X运行程序出现bad interpreter: operation not permitted的解决方案
    C#之枚举类型
    窗体的单例模式
  • 原文地址:https://www.cnblogs.com/zhuyongblogs/p/5206140.html
Copyright © 2020-2023  润新知