首页 > 其他 > 详细

序列化

时间:2014-03-28 22:43:09      阅读:566      评论:0      收藏:0      [点我收藏+]

2014-03-28

本文内容:序列化

概念:

 

序列化:将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取 或反序列化对象的状态,重新创建对象。

需要注意的是对于任何可能包含重要的安全性数据的对象,应该使该对象不可序列化。如果它必须为可序列化的,请尝试生成特定字符来保存不可序列化的重要数据。

常见序列化方式:

二进制序列化、XML序列化、SOAP序列化、JSON序列化(BSON可以与其归为一类)

使用方法(c#)

二进制序列化:

使用前需要在类中加入Serializable特性

 

bubuko.com,布布扣
bubuko.com,布布扣
 [Serializable]
    public class Country
    {
        public string CountryName { get; set; }
        public string CityName { get; set; }
        public string AreaName { get; set; }
    }
View Code
bubuko.com,布布扣

 

bubuko.com,布布扣
bubuko.com,布布扣
           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();
            }
View Code
bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
            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();
            }
View Code
bubuko.com,布布扣

XML序列化:

只对公共类的公共读/写属性,字段; 实现ICollection或IEnumerable的类;xmlElement对象;xmlNode对象;Dataset对象有效

需要在属性中加入XML特性

 

bubuko.com,布布扣
bubuko.com,布布扣
    [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;
View Code
bubuko.com,布布扣

 

SOAP序列化跟二进制差不多

JSON序列化:

可以使用自带的JavaScriptSerializer 命名空间是System.Web.Script.Serialization

也可以使用第三方的Newtonsoft.Json(BSON也在里面)

 

bubuko.com,布布扣
bubuko.com,布布扣
  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)
                );
View Code
bubuko.com,布布扣

 

 

 

 

 



序列化,布布扣,bubuko.com

序列化

原文:http://www.cnblogs.com/shijw/p/3630567.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!