zoukankan      html  css  js  c++  java
  • xml对象的序列化和反序列化

     对象序列化:

    /// <summary>
            /// 将一个对象序列化为XML字符串
            /// </summary>
            /// <param name="o">要序列化的对象</param>
            /// <param name="encoding">编码方式</param>
            /// <returns>序列化产生的XML字符串</returns>
            public static string XmlSerialize(object o, Encoding encoding)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    XmlSerializeInternal(stream, o, encoding);

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

            private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding)
            {
                if (o == null)
                    throw new ArgumentNullException("o");
                if (encoding == null)
                    throw new ArgumentNullException("encoding");

                XmlSerializer serializer = new XmlSerializer(o.GetType(), string.Empty);

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = false;
                //settings.NewLineChars = " ";
                settings.Encoding = encoding;
                settings.IndentChars = "    ";

                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    var ns = new XmlSerializerNamespaces();
                    ns.Add(string.Empty, string.Empty);
                    serializer.Serialize(writer, o, ns);
                    writer.Close();
                }
            }

    对象反序列化:

     /// <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) where T : class
            {
                try
                {
                    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))
                        {
                            var temp = mySerializer.Deserialize(sr);
                            return (T)temp;
                        }
                    }
                }
                catch (Exception ex)
                {               
                    return null;
                }

            }

  • 相关阅读:
    各种各样的Hashes
    爬取时出现报错 encoding error : input conversion failed due to input error, bytes 0xAC 0x4E 0x00 0x00
    Youtube.com中Uncaught TypeError: copy is not a function,如何解决
    生成“2021-Aug-27th”这样的1st,2nd,3rd, 4th 日期格式
    How to clear the content of the recycle bin in PowerShell?
    Delphi开发的服务在Windows2003 64位注册方式
    VS2015打开时,一直处理等待状态,并显示“Microsoft Visual Studio正忙”的提示窗,如下图
    Oracle操作类
    ie浏览器与其他浏览器兼容性问题解决
    echarts实时曲线【转】
  • 原文地址:https://www.cnblogs.com/sk2016/p/6086248.html
Copyright © 2011-2022 走看看