zoukankan      html  css  js  c++  java
  • xml序列化与反序列化

    /// <summary>
            /// 序列化请求数据 格式化字符串,不包含换行字符
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="t"></param>
            /// <returns></returns>
            public static string Serialize<T>(T t)
            {
                MemoryStream stream = new MemoryStream();
                XmlSerializer xml = new XmlSerializer(t.GetType());
                XmlTextWriter writer = new XmlTextWriter(stream, Encoding.GetEncoding("GBK"));
                writer.Formatting = Formatting.None;
                try
                {
                    xml.Serialize(writer, t);
                }
                catch (InvalidOperationException)
                {
                    throw new ApplicationException("序列化异常!");
                }
                StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GBK"));
                stream.Position = 0;
                StringBuilder sb = new StringBuilder();
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    sb.Append(line);
                }
                reader.Close();
                writer.Close();
                return sb.ToString(); ;
            }
    /// <summary>
            /// 序列化请求数据 未格式化字符串  包含换行符 
    
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="t"></param>
            /// <returns></returns>
            public static string Serialize<T>(T t)
            {
                MemoryStream stream = new MemoryStream();
                XmlSerializer xml = new XmlSerializer(t.GetType());
                //XmlTextWriter writer = new XmlTextWriter(stream, Encoding.GetEncoding("GBK"));
                StreamWriter writer = new StreamWriter(stream, Encoding.GetEncoding("GBK"));
                //writer.Formatting = Formatting.None;
                try
                {
                    xml.Serialize(writer, t);
                }
                catch (InvalidOperationException)
                {
                    throw new ApplicationException("序列化异常!");
                }
                StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GBK"));
                //stream.Position = 0;
                //StringBuilder sb = new StringBuilder();
                //string line;
                //while ((line = reader.ReadLine()) != null)
                //{
                //    sb.Append(line);
                //}
                //reader.Close();
                //writer.Close();
                stream.Position = 0;
                string sr = reader.ReadToEnd();
                string head = "xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"";
                string strs = sr.Replace(head, "");
                return strs.ToString(); 
            }
    /// <summary>
            /// 反序列化响应内容(反序列化为对象)
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="responseData"></param>
            /// <returns></returns>
            public static T Deserialize<T>(string responseData)
            {
                XmlSerializer xmls = new XmlSerializer(typeof(T));
                StringReader reader;
                T result=default(T);
                try
                {
                    reader = new StringReader(responseData);
                    result= (T)xmls.Deserialize(reader);
                }
                catch (Exception ex) { }
                return result;
            }
  • 相关阅读:
    大话算法-排序-希尔排序
    Linux三剑客-awk
    Linux三剑客-sed
    大话算法-排序-冒泡排序
    大话算法-排序-选择排序
    删除Win10菜单中的幽灵菜单(ms-resource:AppName/Text )
    微信推送模板消息
    获取当前域名的根域
    MVC 全站开启缓存,缓解服务器的请求压力
    MVC 开启gzip压缩
  • 原文地址:https://www.cnblogs.com/z-huan/p/7325718.html
Copyright © 2011-2022 走看看