2014-03-28
本文内容:序列化
概念:
序列化:将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取 或反序列化对象的状态,重新创建对象。
需要注意的是对于任何可能包含重要的安全性数据的对象,应该使该对象不可序列化。如果它必须为可序列化的,请尝试生成特定字符来保存不可序列化的重要数据。
常见序列化方式:
二进制序列化、XML序列化、SOAP序列化、JSON序列化(BSON可以与其归为一类)
使用方法(c#)
二进制序列化:
使用前需要在类中加入Serializable特性
[Serializable] public class Country { public string CountryName { get; set; } public string CityName { get; set; } public string AreaName { get; set; } }
List<Country> list = new List<Country>(); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "滨江区" }); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "上城区" }); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "下沙区" }); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "西湖区" }); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "下城区" }); Stream fs = new FileStream(@"a.dat", FileMode.Create,FileAccess.Write,FileShare.None); try { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, list); } catch (SerializationException se) { Console.WriteLine(se.Message); throw; } finally { fs.Close(); }
Stream fs = new FileStream(@"a.dat", FileMode.Open,FileAccess.Read,FileShare.Read); try { BinaryFormatter bf = new BinaryFormatter(); List<Country> clist = bf.Deserialize(fs) as List<Country>; clist.ForEach( c => Console.WriteLine(c.CountryName + " " + c.CityName + " " + c.AreaName) ); } catch(SerializationException se) { Console.WriteLine(se.Message); throw; } finally { fs.Close(); }
XML序列化:
只对公共类的公共读/写属性,字段; 实现ICollection或IEnumerable的类;xmlElement对象;xmlNode对象;Dataset对象有效
需要在属性中加入XML特性
[XmlRoot("XCountry")] public class XCountry { [XmlAttribute("CountryName")] public string CountryName { get; set; } [XmlElement("CityName")] public string CityName { get; set; } [XmlAttribute("AreaName")] public string AreaName { get; set; } } XCountry xc = new XCountry(); xc.CountryName = "浙江省"; xc.CityName = "杭州市"; xc.AreaName = "滨江区"; XmlSerializer xs = new XmlSerializer(typeof(XCountry)); xs.Serialize(File.Create(@"b.dat"), xc); XmlSerializer xs = new XmlSerializer(typeof(XCountry)); XCountry xc = xs.Deserialize(File.OpenRead(@"b.dat")) as XCountry;
SOAP序列化跟二进制差不多
JSON序列化:
可以使用自带的JavaScriptSerializer 命名空间是System.Web.Script.Serialization
也可以使用第三方的Newtonsoft.Json(BSON也在里面)
List<Country> list = new List<Country>(); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "滨江区" }); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "上城区" }); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "下沙区" }); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "西湖区" }); list.Add(new Country { CountryName = "浙江省", CityName = "杭州市", AreaName = "下城区" }); string json = JsonConvert.SerializeObject(list); var lc = JsonConvert.DeserializeObject<List<Country>>(json); lc.ForEach( c => Console.WriteLine(c.CountryName + " " + c.CityName + " " + c.AreaName) );
原文:http://www.cnblogs.com/shijw/p/3630567.html