zoukankan      html  css  js  c++  java
  • XML序列化、反序列化

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Web;
    using System.Xml.Serialization;
    
    namespace Interfaces_Helper
    {
        /// <summary>
        /// Xml序列化与反序列化
        /// </summary>
        public class XmlUtil
        {
            #region 反序列化
            /// <summary>
            /// 反序列化
            /// </summary>
            /// <param name="type">类型</param>
            /// <param name="xml">XML字符串</param>
            /// <returns></returns>
            public static object Deserialize(Type type, string xml)
            {
                try
                {
                    using (StringReader sr = new StringReader(xml))
                    {
                        XmlSerializer xmldes = new XmlSerializer(type);
                        return xmldes.Deserialize(sr);
                    }
                }
                catch
                {
    
                    return null;
                }
            }
            /// <summary>
            /// 反序列化
            /// </summary>
            /// <param name="type"></param>
            /// <param name="xml"></param>
            /// <returns></returns>
            public static object Deserialize(Type type, Stream stream)
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(stream);
            }
            
            #endregion
    
            #region 序列化
            /// <summary>
            /// 序列化
            /// </summary>
            /// <param name="type">类型</param>
            /// <param name="obj">对象</param>
            /// <returns></returns>
            public static string Serializer(Type type, object obj)
            {
                MemoryStream Stream = new MemoryStream();
                XmlSerializer xml = new XmlSerializer(type);
                try
                {
                    //序列化对象
                    xml.Serialize(Stream, obj);
                }
                catch (InvalidOperationException)
                {
                    throw;
                }
                Stream.Position = 0;
                StreamReader sr = new StreamReader(Stream);
                string str = sr.ReadToEnd();
    
                sr.Dispose();
                Stream.Dispose();
    
                return str;
            }
    
            #endregion
    
            #region BinaryFormatter序列化
            /// <summary>
            /// BinaryFormatter序列化
            /// </summary>
            /// <param name="item">对象</param>
            public static string ToBinary<T>(T item)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream())
                {
                    formatter.Serialize(ms, item);
                    ms.Position = 0;
                    byte[] bytes = ms.ToArray();
                    StringBuilder sb = new StringBuilder();
                    foreach (byte bt in bytes)
                    {
                        sb.Append(string.Format("{0:X2}", bt));
                    }
                    return sb.ToString();
                }
            }
    
            /// <summary>
            /// BinaryFormatter反序列化
            /// </summary>
            /// <param name="str">字符串序列</param>
            public static T FromBinary<T>(string str)
            {
                int intLen = str.Length / 2;
                byte[] bytes = new byte[intLen];
                for (int i = 0; i < intLen; i++)
                {
                    int ibyte = Convert.ToInt32(str.Substring(i * 2, 2), 16);
                    bytes[i] = (byte)ibyte;
                }
                BinaryFormatter formatter = new BinaryFormatter();
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    return (T)formatter.Deserialize(ms);
                }
            }
            #endregion
        }
    }
    

      

  • 相关阅读:
    洛谷 P1886 滑动窗口(单调队列)
    POJ 2559 Largest Rectangle in a Histogram(单调栈)
    eclipse开发velocity实例(初学)
    Spring MVC 教程,快速入门,深入分析
    传智博客(JavaWeb方面的所有知识)听课记录(经典)
    JSP/SERVLET入门教程--Servlet 使用入门
    javaweb入门实例---servlet例子
    Eclipse快捷键大全(转载)
    简单java web应用程序搭建与部署
    Servlet 工作原理解析
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/6419371.html
Copyright © 2011-2022 走看看