zoukankan      html  css  js  c++  java
  • 对象序列化反序列化

           //方法1   如在webservice中  序列化
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, jcar);
                byte[] buffer = ms.ToArray();
                return buffer ;


               在接收端得到buffer反序列化

                BinaryFormatter bf2 = new BinaryFormatter();
                MemoryStream ms2 = new MemoryStream(buffer);
                JamesBoudCar j = (JamesBoudCar)bf2.Deserialize(ms2);
                Response.Write("汽车可以飞吗?:{0}" + j.canFly);
                Response.Write("汽车可以在水里开吗?:{0}" + j.canSubmerge);




    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization.Formatters.Soap;
    using System.Xml.Serialization;
    namespace Object_DxXuLieHua_17
    {
        /// <summary>
        /// 对象序列化
        ///
        /// </summary>
        [Serializable]
        public class Redio
        {
            public bool hasTweeters;
            public bool hasSubWoofers;
            public double[] stationPresets;
            [NonSerialized]
            public string radioID = "XF-552RR6";
        }
        [Serializable]
        public class Car
        {
            public Redio theRedio = new Redio();
            public bool isHatchBack;
        }
        [Serializable,XmlRoot(Namespace="http://www.intertechtraining.com")]
        public class JamesBoudCar : Car
        {
            [XmlAttribute]
            public bool canFly;
            [XmlAttribute]
            public bool canSubmerge;
            public JamesBoudCar(bool sky, bool sea)
            {
                canFly = sky;
                canSubmerge = sea;
            }
            public JamesBoudCar() { }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("对象序列化");
                //要序列化的对象
                JamesBoudCar jcar = new JamesBoudCar();
                jcar.canFly = true;
                jcar.canSubmerge = false;
                jcar.theRedio.stationPresets = new double[] {89.3,105.1,97.1 };
                jcar.isHatchBack = true;


                #region 使用BinaryFormatter序列化反序列化对象
                //将对象以二进制保存到一个文件
                BinaryFormatter bin = new BinaryFormatter();
                Stream fstream = new FileStream("CarData.dat",FileMode.Create,FileAccess.Write,FileShare.None);
                bin.Serialize(fstream,jcar);
                fstream.Close();
                Console.ReadLine();
                //反序列化
                fstream = File.OpenRead("CarData.dat");
                JamesBoudCar j = (JamesBoudCar)bin.Deserialize(fstream);
                Console.WriteLine("汽车可以飞吗?:{0}",j.canFly);
                Console.WriteLine("汽车可以在水里开吗?:{0}",j.canSubmerge);
                fstream.Close();
                Console.ReadLine();
                #endregion

                #region 使用SoapFprmatter序列化反序列化对象
                //对象以SOAP格式保存
                SoapFormatter soap = new SoapFormatter();
                fstream = new FileStream("CarData.soap",FileMode.Create,FileAccess.Write,FileShare.None);
                soap.Serialize(fstream,jcar);
                fstream.Close();
                Console.ReadLine();
                //反序列化
                fstream = File.OpenRead("CarData.soap");
                JamesBoudCar jc = (JamesBoudCar)soap.Deserialize(fstream);
                Console.WriteLine("查看数字1:{0}",jc.theRedio.stationPresets[0]);
                Console.WriteLine("查看数字2:{0}", jc.theRedio.stationPresets[1]);
                fstream.Close();
                Console.ReadLine();
                #endregion

                #region 使用XMLSerializer序列化反序列化对象
                //对象以XML格式存储
                XmlSerializer xml = new XmlSerializer(typeof(JamesBoudCar), new Type[] { typeof(Redio),typeof(Car)});
                fstream =new FileStream("CarData.xml",FileMode.Create,FileAccess.Write,FileShare.None);
                xml.Serialize(fstream,jcar);
                fstream.Close();
                Console.ReadLine();
                //反序列化
                fstream = File.OpenRead("CarData.xml");
                JamesBoudCar jj = (JamesBoudCar)xml.Deserialize(fstream);
                Console.WriteLine("第三个数字是:{0}",jj.theRedio.stationPresets[2]);
                Console.WriteLine("车的号码是:{0}",jj.theRedio.radioID);
                fstream.Close();
                Console.ReadLine();
                #endregion

                #region 使用XMLSerializer序列化反序列化对象集合
                //对象以XML格式存储
                List<JamesBoudCar> list = new List<JamesBoudCar>();
                list.Add(new JamesBoudCar(true,false));
               //list.Add(new JamesBoudCar(true, true));
               //list.Add(new JamesBoudCar(false, false));
               // list[0].canFly = false;
                list[0].canSubmerge = false;
                list[0].isHatchBack = true;
                list[0].theRedio.stationPresets = new double[] {12.1,22.6,456.3};
                fstream = new FileStream("CarConnection.xml",FileMode.Create,FileAccess.Write,FileShare.None);
                xml = new XmlSerializer(typeof(List<JamesBoudCar>), new Type[] { typeof(JamesBoudCar),typeof(Redio),typeof(Car)});
                xml.Serialize(fstream,list);
                fstream.Close();
                Console.ReadLine();
                //反序列化
                fstream = File.OpenRead("CarConnection.xml");
                List<JamesBoudCar> lis =xml.Deserialize(fstream) as List<JamesBoudCar>;
                Console.WriteLine("查看数字一:{0}",lis[0].theRedio.stationPresets[0]);
                Console.WriteLine("汽车能飞吗?:{0}",lis[0].canFly);
                #endregion

            }
        }
    }

     

  • 相关阅读:
    cocos2d-x 3.0游戏实例学习笔记 《跑酷》第七步--物理碰撞检測(1)
    Android给定坐标计算距离
    LeetCode——Gray Code
    hdu 1203
    2015 年度新增开源软件排名TOP100
    compact处理流程分析
    ExtJs--06--Ext.WindowGroup相关方法简单使用
    android4.4的两个bug
    高阶MapReduce_1_链接多个MapReduce作业
    如何设置eclipse格式化xml代码时不自动换行
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1541086.html
Copyright © 2011-2022 走看看