SerializationEx.cs
上传用户:hpq1101
上传日期:2022-05-15
资源大小:56k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Others

  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.Runtime.Serialization.Formatters.Soap;
  5. using System.Xml.Serialization;
  6. using System.Runtime.Serialization.Json;
  7. using System.Text;
  8.  
  9. using System.Runtime.Serialization;
  10. using System.ServiceModel.Web;
  11. using System.Json;
  12. namespace 序列化与反序列化
  13. {
  14.     class SerializationEx
  15.     {
  16.         /// <summary>
  17.         /// 应用程序的主入口点。
  18.         /// </summary>
  19.         [STAThread]
  20.         static void Main()
  21.         { Console.WriteLine("****************二进制***************************");
  22.             {
  23.                 UserInfo user = new UserInfo("小雨", 26, true);
  24.                 //执行二进制序列化
  25.                 BinarySerialize(user);
  26.                 //执行二进制反序列化
  27.                 UserInfo user2 = BinaryDeserialize();
  28.                 Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", user2.Name, user2.Age, user2.IsVIP, user2.ShowDes());
  29.             }
  30.            Console.WriteLine("***************SOAP****************************");
  31.             {
  32.                 UserInfo user = new UserInfo("小雨", 26, true);
  33.                 //执行二进制序列化
  34.                 SOAPSerialize (user);
  35.                 //执行二进制反序列化
  36.                 UserInfo user2 = SOAPDeserialize();
  37.                 Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", user2.Name, user2.Age, user2.IsVIP, user2.ShowDes());
  38.             }
  39.             Console.WriteLine("*******************XML************************");
  40.             {
  41.                 UserInfo user = new UserInfo("小王", 27, false);
  42.                 XmlSerializer serialsizer = new XmlSerializer(typeof(UserInfo));
  43.                 //执行XML序列化过程
  44.                 StreamWriter sw = new StreamWriter("MySerialze.xml");
  45.                 serialsizer.Serialize(sw, user);
  46.                 sw.Close();
  47.                 //执行XML反序列化过程
  48.                 FileStream fs = new FileStream("MySerialze.xml", FileMode.Open);
  49.                 UserInfo user2 = serialsizer.Deserialize(fs) as UserInfo;
  50.                 Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", user2.Name, user2.Age, user2.IsVIP, user2.ShowDes());
  51.                 DataSerialize(user);
  52.                 JsonSerialize(user);
  53.             } 
  54.           
  55.             Console.Read();
  56.         }
  57.         //实现二进制序列化
  58.         public static void BinarySerialize(UserInfo ui)
  59.         {
  60.             FileStream fs = new FileStream("MySerialze.ss", FileMode.Create);
  61.             BinaryFormatter formatter = new BinaryFormatter();
  62.             formatter.Serialize(fs, ui);
  63.             fs.Close();
  64.         }
  65.         //实现二进制反序列化
  66.         public static UserInfo BinaryDeserialize()
  67.         {
  68.             FileStream fs = new FileStream("Myserialze.ss", FileMode.Open, FileAccess.Read, FileShare.Read);
  69.             BinaryFormatter formatter = new BinaryFormatter();
  70.             UserInfo user = formatter.Deserialize(fs) as UserInfo;
  71.             fs.Close();
  72.             return user;
  73.         }
  74.         /// <summary>   
  75.         /// soap化对象   
  76.         /// </summary>   
  77.         /// <param name="obj"></param>   
  78.         public static void SOAPSerialize(UserInfo obj)
  79.         {
  80.             using (FileStream stream = new FileStream("MyObject.soap", FileMode.Create, FileAccess.Write))
  81.             {
  82.                 SoapFormatter formater = new SoapFormatter();
  83.                 formater.Serialize(stream, obj);
  84.               
  85.             }
  86.         }
  87.         /// <summary>   
  88.         /// soap反序列化   
  89.         /// </summary>   
  90.         /// <param name="fileName"></param>   
  91.         public static UserInfo SOAPDeserialize()
  92.         {
  93.             using (FileStream stream = new FileStream("MyObject.soap", FileMode.Open, FileAccess.Read))
  94.             {
  95.                 SoapFormatter formater = new SoapFormatter();
  96.                 UserInfo obj = (UserInfo)formater.Deserialize(stream);
  97.                 return obj;
  98.             }
  99.         }
  100.         ///json序列化开始
  101.         public static void JsonSerialize(UserInfo obj)
  102.         {
  103.             
  104.             MemoryStream ms = new MemoryStream();
  105.             DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(UserInfo ));
  106.             ser.WriteObject(ms, obj);
  107.             byte[] json = ms.ToArray();
  108.             ms.Close();
  109.             string jsonString = Encoding.UTF8.GetString(json, 0, json.Length);//序列化得到的字符串
  110.        //json字符串解析(相当于反序列化)
  111.             JsonValue jsonv = JsonObject.Parse(jsonString);
  112.             UserInfo pTest = new UserInfo() { Age = int.Parse(jsonv["Age"].ToString()), Name = jsonv["Name"].ToString() };
  113.             Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", pTest.Name, pTest.Age, pTest.IsVIP, pTest.ShowDes());
  114.       
  115.         }
  116.         public static void DataSerialize(UserInfo obj)
  117.         {
  118.             //DataContract方式序列化
  119.             MemoryStream ms = new MemoryStream();
  120.             DataContractSerializer ser = new DataContractSerializer(typeof(UserInfo));
  121.             ser.WriteObject(ms, obj);
  122.             byte[] array = ms.ToArray();
  123.             ms.Close();
  124.             string _serializeString = Encoding.UTF8.GetString(array, 0, array.Length);
  125.             //反序列化
  126.             DataContractSerializer ser2 = new DataContractSerializer(typeof(UserInfo));
  127.             MemoryStream ms2 = new MemoryStream(Encoding.UTF8.GetBytes(_serializeString));
  128.             UserInfo pTest = ser2.ReadObject(ms2) as UserInfo;
  129.             //显示反序列化后的结果
  130.           //  Console.WriteLine("DataContract序列化后的字符串:(长度" + _serializeString.Length + ")n" + _serializeString + "nn反序列化后的结果:nAge=" + pTest.Age + ",Name=" + pTest.Name);
  131.             Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", pTest.Name, pTest.Age, pTest.IsVIP, pTest.ShowDes());
  132.         }
  133.     }
  134.     [Serializable]
  135.     public class UserInfo
  136.     {
  137.         [NonSerialized] 
  138.         public string Name;
  139.         public Int32 Age;
  140.         public bool IsVIP;
  141.         private string des = "I'm Jay";
  142.  
  143.         public UserInfo()
  144.         {
  145.         }
  146.         public UserInfo(string name, Int32 age, bool isVip)
  147.         {
  148.             Name = name;
  149.             Age = age;
  150.             IsVIP = isVip;
  151.         }
  152.         public string ShowDes()
  153.         {
  154.             return des;
  155.         }
  156.     }
  157.     [global::System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
  158.     sealed class MyAttribute : Attribute
  159.     {
  160.         // See the attribute guidelines at 
  161.         //  http://go.microsoft.com/fwlink/?LinkId=85236
  162.         readonly string positionalString;
  163.         // This is a positional argument
  164.         public MyAttribute(string positionalString)
  165.         {
  166.             this.positionalString = positionalString;
  167.             // TODO: Implement code here
  168.             throw new NotImplementedException();
  169.         }
  170.         public string PositionalString
  171.         {
  172.             get { return positionalString; }
  173.         }
  174.         // This is a named argument
  175.         public int NamedInt { get; set; }
  176.     }
  177. }