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
        }
    }
    

      

  • 相关阅读:
    Linux-通过破解Root密码来更改用户密码
    测验1:Python基本语法元素(第一周)
    Visual Studio 2019的安装&编写第一个C程序
    STL-vector之邻接表的建立
    分块-区间求和
    代码阅读方法与实践阅读笔记04
    代码阅读方法与实践阅读笔记03
    代码阅读方法与实践阅读笔记02
    代码阅读方法与实践阅读笔记01
    我们应当怎样做需求分析--阅读笔记
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/6419371.html
Copyright © 2011-2022 走看看