.net序列化与反序列化包括XML,SOAP,Binary,JSON

  1. using System;   
  2. using System.IO;   
  3. using System.Runtime.Serialization.Formatters.Binary;   
  4. using System.Runtime.Serialization.Formatters.Soap;   
  5. using System.Runtime.Serialization.Json;   
  6. using System.Text;   
  7. using System.Xml;   
  8. using System.Xml.Serialization;   
  9.   
  10. namespace Common.Serialization   
  11. {   
  12.     class SerializeHelper   
  13.     {  
  14.         #region XmlSerializer   
  15.         public string ToXml<T>(T item)   
  16.         {   
  17.             XmlSerializer serializer = new XmlSerializer(item.GetType());   
  18.             StringBuilder sb = new StringBuilder();   
  19.             using (XmlWriter writer = XmlWriter.Create(sb))   
  20.             {   
  21.                 serializer.Serialize(writer, item);   
  22.                 return sb.ToString();   
  23.             }   
  24.         }   
  25.   
  26.         public T FromXml<T>(string str)   
  27.         {   
  28.             XmlSerializer serializer = new XmlSerializer(typeof(T));   
  29.             using (XmlReader reader = new XmlTextReader(new StringReader(str)))   
  30.             {   
  31.                 return (T)serializer.Deserialize(reader);   
  32.             }   
  33.         }  
  34.         #endregion  
  35.  
  36.         #region BinaryFormatter   
  37.         public string ToBinary<T>(T item)   
  38.         {   
  39.             BinaryFormatter formatter = new BinaryFormatter();   
  40.             using (MemoryStream ms = new MemoryStream())   
  41.             {   
  42.                 formatter.Serialize(ms, item);   
  43.                 ms.Position = 0;   
  44.                 byte[] bytes = ms.ToArray();   
  45.                 StringBuilder sb = new StringBuilder();   
  46.                 foreach (byte bt in bytes)   
  47.                 {   
  48.                     sb.Append(string.Format("{0:X2}", bt));   
  49.                 }   
  50.                 return sb.ToString();   
  51.             }   
  52.         }   
  53.   
  54.         public T FromBinary<T>(string str)   
  55.         {   
  56.             int intLen = str.Length / 2;   
  57.             byte[] bytes = new byte[intLen];   
  58.             for (int i = 0; i < intLen; i++)   
  59.             {   
  60.                 int ibyte = Convert.ToInt32(str.Substring(i * 2, 2), 16);   
  61.                 bytes[i] = (byte)ibyte;   
  62.             }   
  63.             BinaryFormatter formatter = new BinaryFormatter();   
  64.             using (MemoryStream ms = new MemoryStream(bytes))   
  65.             {   
  66.                 return (T)formatter.Deserialize(ms);   
  67.             }   
  68.         }  
  69.         #endregion  
  70.  
  71.         #region SoapFormatter   
  72.         public string ToSoap<T>(T item)   
  73.         {   
  74.             SoapFormatter formatter = new SoapFormatter();   
  75.             using (MemoryStream ms = new MemoryStream())   
  76.             {   
  77.                 formatter.Serialize(ms, item);   
  78.                 ms.Position = 0;   
  79.                 XmlDocument xmlDoc = new XmlDocument();   
  80.                 xmlDoc.Load(ms);   
  81.                 return xmlDoc.InnerXml;   
  82.             }   
  83.         }   
  84.   
  85.         public T FromSoap<T>(string str)   
  86.         {   
  87.             XmlDocument xmlDoc = new XmlDocument();   
  88.             xmlDoc.LoadXml(str);   
  89.             SoapFormatter formatter = new SoapFormatter();   
  90.             using (MemoryStream ms = new MemoryStream())   
  91.             {   
  92.                 xmlDoc.Save(ms);   
  93.                 ms.Position = 0;   
  94.                 return (T)formatter.Deserialize(ms);   
  95.             }   
  96.         }  
  97.         #endregion  
  98.  
  99.  
  100.         #region JsonSerializer   
  101.         public string ToJson<T>(T item)   
  102.         {   
  103.             DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());   
  104.             using (MemoryStream ms = new MemoryStream())   
  105.             {   
  106.                 serializer.WriteObject(ms, item);   
  107.                 return Encoding.UTF8.GetString(ms.ToArray());   
  108.             }   
  109.         }   
  110.   
  111.         public T FromJson<T>(string str) where T : class  
  112.         {   
  113.             DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));   
  114.             using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))   
  115.             {   
  116.                 return serializer.ReadObject(ms) as T;   
  117.             }   
  118.         }  
  119.         #endregion   
  120.   
  121.     }   
  122. }  

 以上这些都是在微软提供的类库上创建的,调用时需要引用命名空间。

网上相关测试表明,JSON.net速度至少强于微软提供的JSON速度2倍,且支持Linq to JSON,如有需要请参见:

JSON.net官方网站:http://james.newtonking.com/projects/json-net.aspx
JSON.net介绍:http://www.cnblogs.com/jams742003/archive/2009/11/04/1595737.html

原文地址:https://www.cnblogs.com/liufei88866/p/1762672.html