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

      

  • 相关阅读:
    Silverlight C# 游戏开发:Flyer05与什么什么进行搏斗
    Silverlight C# 游戏开发:Flyer07做一个有开始的游戏
    Silverlight C# 游戏开发:面向对象在游戏中的实例(一)
    Silverlight C# 游戏开发:面向对象在游戏中的实例(二)
    Silverlight C# 游戏开发:Flyer06小小的改进让游戏更有趣
    linux网络命令ip\route\links回顾
    Google Style的C++编码规范
    TCP/IP协议和IP组播的视频传输
    Multicast server and client in Python
    用户profile中umask码的含义详解(默认是022)
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/6419371.html
Copyright © 2020-2023  润新知