序列化:序列化指的是 将对象 通过流的方式 保存为一个文件。 反序列化则是将该文件还原成 对象的过程。
序列化的作用:序列化可以跨语言跨平台 传输数据,将某一对象序列化成通用的文件格式在进行传输。 比如XML 等。
C# 中的序列化方法有一下4种:
1.BinaryFormatter
2.SoapFormatter(.net 3.5 开始便废弃它了,不要在生产代码中使用它)
3.XmlSerializer
4.Json
首先查看 BinaryFormatter 序列化 反序列化
//Serializable 标示可序列化 [Serializable] public class Cars { public ArrayList carBrandList; //品牌数组 public Cars() { carBrandList = new ArrayList(); } public string CarName; [NonSerialized] //标示不可序列化 public string carNumber; public string CarID { get; set; } private string _carPrice; public void SetCarPrice(string price) { _carPrice = price; } } static void Main(string[] args) { //实例化对象 Cars cars = new Cars(); cars.CarID = "001"; cars.carBrandList.Add("宝马"); cars.carBrandList.Add("路虎"); cars.carBrandList.Add("布加迪"); cars.CarName = "路虎-揽胜极光"; cars.carNumber = "闽A-0888A"; cars.SetCarPrice("590000.00"); //BinaryFormatter 序列化 string strFile = "c:\\cars.txt"; using (FileStream fs = new FileStream(strFile, FileMode.Create)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, cars); } Cars car = DeSerialize(); // } //反序列化 public Cars DeSerialize() { string strFile = "c:\\cars.txt"; Cars cars; using (FileStream fs = new FileStream(strFile, FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); cars = (Cars)formatter.Deserialize(fs); } return cars; }
接下来查看下 反序列化回来的对象属性
SoapFormatter序列化反序列化
//Serializable 标示可序列化 [Serializable] public class Cars { public ArrayList carBrandList; //品牌数组 public Cars() { carBrandList = new ArrayList(); } public string CarName; [NonSerialized] //标示不可序列化 public string carNumber; public string CarID { get; set; } private string _carPrice; public void SetCarPrice(string price) { _carPrice = price; } } static void Main(string[] args) { //SoapFormatter 序列化 string strSoapFile = "c:\\cars.Soap"; using (FileStream fs = new FileStream(strSoapFile, FileMode.Create)) { SoapFormatter formatter = new SoapFormatter(); formatter.Serialize(fs, cars); } Cars scar = DeSoapSerialize(); } //反序列化 public Cars DeSoapSerialize() { string strFile = "c:\\cars.Soap"; Cars cars; using (FileStream fs = new FileStream(strFile, FileMode.Open)) { SoapFormatter formatter = new SoapFormatter(); cars = (Cars)formatter.Deserialize(fs); } return cars; }
XmlSerializer 序列化 反序列化
//Serializable 标示可序列化 [Serializable] public class Cars { public ArrayList carBrandList; //品牌数组 public Cars() { carBrandList = new ArrayList(); } public string CarName; [NonSerialized] //标示不可序列化 public string carNumber; public string CarID { get; set; } private string _carPrice; public void SetCarPrice(string price) { _carPrice = price; } } static void Main(string[] args) { //实例化对象 Cars cars = new Cars(); cars.CarID = "001"; cars.carBrandList.Add("宝马"); cars.carBrandList.Add("路虎"); cars.carBrandList.Add("布加迪"); cars.CarName = "路虎-揽胜极光"; cars.carNumber = "闽A-0888A"; cars.SetCarPrice("590000.00"); //XmlSerializer 序列化 string strXMLFile = "c:\\cars.XML"; using (FileStream fs = new FileStream(strXMLFile, FileMode.Create)) { XmlSerializer serializer = new XmlSerializer(typeof(Cars)); serializer.Serialize(fs, cars); } Cars xcar = DeXMLSerialize(); } public Cars DeXMLSerialize() { string strFile = "c:\\cars.XML"; Cars cars; using (FileStream fs = new FileStream(strFile, FileMode.Open)) { XmlSerializer formatter = new XmlSerializer(typeof(Cars)); cars = (Cars)formatter.Deserialize(fs); } return cars; }
都执行完成后 大家可以查看下生产的 的 文件,查看下反序列化出来的结果 可以发现
1.单有[NonSerialized] 表示的时候 BinaryFormatter 与 Soap 是无法序列化对象属性。
2.private 私有属性 XML方式是无法序列化的。
3.XML可序列化[NonSerialized]标示的 属性。