SerializationEx.cs
资源名称:xuliehua.rar [点击查看]
上传用户:hpq1101
上传日期:2022-05-15
资源大小:56k
文件大小:7k
源码类别:
xml/soap/webservice
开发平台:
Others
- using System;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Runtime.Serialization.Formatters.Soap;
- using System.Xml.Serialization;
- using System.Runtime.Serialization.Json;
- using System.Text;
- using System.Runtime.Serialization;
- using System.ServiceModel.Web;
- using System.Json;
- namespace 序列化与反序列化
- {
- class SerializationEx
- {
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- { Console.WriteLine("****************二进制***************************");
- {
- UserInfo user = new UserInfo("小雨", 26, true);
- //执行二进制序列化
- BinarySerialize(user);
- //执行二进制反序列化
- UserInfo user2 = BinaryDeserialize();
- Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", user2.Name, user2.Age, user2.IsVIP, user2.ShowDes());
- }
- Console.WriteLine("***************SOAP****************************");
- {
- UserInfo user = new UserInfo("小雨", 26, true);
- //执行二进制序列化
- SOAPSerialize (user);
- //执行二进制反序列化
- UserInfo user2 = SOAPDeserialize();
- Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", user2.Name, user2.Age, user2.IsVIP, user2.ShowDes());
- }
- Console.WriteLine("*******************XML************************");
- {
- UserInfo user = new UserInfo("小王", 27, false);
- XmlSerializer serialsizer = new XmlSerializer(typeof(UserInfo));
- //执行XML序列化过程
- StreamWriter sw = new StreamWriter("MySerialze.xml");
- serialsizer.Serialize(sw, user);
- sw.Close();
- //执行XML反序列化过程
- FileStream fs = new FileStream("MySerialze.xml", FileMode.Open);
- UserInfo user2 = serialsizer.Deserialize(fs) as UserInfo;
- Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", user2.Name, user2.Age, user2.IsVIP, user2.ShowDes());
- DataSerialize(user);
- JsonSerialize(user);
- }
- Console.Read();
- }
- //实现二进制序列化
- public static void BinarySerialize(UserInfo ui)
- {
- FileStream fs = new FileStream("MySerialze.ss", FileMode.Create);
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(fs, ui);
- fs.Close();
- }
- //实现二进制反序列化
- public static UserInfo BinaryDeserialize()
- {
- FileStream fs = new FileStream("Myserialze.ss", FileMode.Open, FileAccess.Read, FileShare.Read);
- BinaryFormatter formatter = new BinaryFormatter();
- UserInfo user = formatter.Deserialize(fs) as UserInfo;
- fs.Close();
- return user;
- }
- /// <summary>
- /// soap化对象
- /// </summary>
- /// <param name="obj"></param>
- public static void SOAPSerialize(UserInfo obj)
- {
- using (FileStream stream = new FileStream("MyObject.soap", FileMode.Create, FileAccess.Write))
- {
- SoapFormatter formater = new SoapFormatter();
- formater.Serialize(stream, obj);
- }
- }
- /// <summary>
- /// soap反序列化
- /// </summary>
- /// <param name="fileName"></param>
- public static UserInfo SOAPDeserialize()
- {
- using (FileStream stream = new FileStream("MyObject.soap", FileMode.Open, FileAccess.Read))
- {
- SoapFormatter formater = new SoapFormatter();
- UserInfo obj = (UserInfo)formater.Deserialize(stream);
- return obj;
- }
- }
- ///json序列化开始
- public static void JsonSerialize(UserInfo obj)
- {
- MemoryStream ms = new MemoryStream();
- DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(UserInfo ));
- ser.WriteObject(ms, obj);
- byte[] json = ms.ToArray();
- ms.Close();
- string jsonString = Encoding.UTF8.GetString(json, 0, json.Length);//序列化得到的字符串
- //json字符串解析(相当于反序列化)
- JsonValue jsonv = JsonObject.Parse(jsonString);
- UserInfo pTest = new UserInfo() { Age = int.Parse(jsonv["Age"].ToString()), Name = jsonv["Name"].ToString() };
- Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", pTest.Name, pTest.Age, pTest.IsVIP, pTest.ShowDes());
- }
- public static void DataSerialize(UserInfo obj)
- {
- //DataContract方式序列化
- MemoryStream ms = new MemoryStream();
- DataContractSerializer ser = new DataContractSerializer(typeof(UserInfo));
- ser.WriteObject(ms, obj);
- byte[] array = ms.ToArray();
- ms.Close();
- string _serializeString = Encoding.UTF8.GetString(array, 0, array.Length);
- //反序列化
- DataContractSerializer ser2 = new DataContractSerializer(typeof(UserInfo));
- MemoryStream ms2 = new MemoryStream(Encoding.UTF8.GetBytes(_serializeString));
- UserInfo pTest = ser2.ReadObject(ms2) as UserInfo;
- //显示反序列化后的结果
- // Console.WriteLine("DataContract序列化后的字符串:(长度" + _serializeString.Length + ")n" + _serializeString + "nn反序列化后的结果:nAge=" + pTest.Age + ",Name=" + pTest.Name);
- Console.WriteLine("Name:{0}nAge:{1}nVip:{2}nDes:{3}", pTest.Name, pTest.Age, pTest.IsVIP, pTest.ShowDes());
- }
- }
- [Serializable]
- public class UserInfo
- {
- [NonSerialized]
- public string Name;
- public Int32 Age;
- public bool IsVIP;
- private string des = "I'm Jay";
- public UserInfo()
- {
- }
- public UserInfo(string name, Int32 age, bool isVip)
- {
- Name = name;
- Age = age;
- IsVIP = isVip;
- }
- public string ShowDes()
- {
- return des;
- }
- }
- [global::System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
- sealed class MyAttribute : Attribute
- {
- // See the attribute guidelines at
- // http://go.microsoft.com/fwlink/?LinkId=85236
- readonly string positionalString;
- // This is a positional argument
- public MyAttribute(string positionalString)
- {
- this.positionalString = positionalString;
- // TODO: Implement code here
- throw new NotImplementedException();
- }
- public string PositionalString
- {
- get { return positionalString; }
- }
- // This is a named argument
- public int NamedInt { get; set; }
- }
- }