zoukankan      html  css  js  c++  java
  • .NET 二进制序列化器,SOAP序列化器,XML序列化器

    这里就不说JSON序列化了,只介绍三种:二进制序列化器,SOAP序列化器,XML序列化器

    直接上代码:

    /// <summary>
    /// 二进制序列化器、
    /// 最节省流量,压缩程度最大的序列化器
    /// </summary>
    public static void BinarySerialize()
    {
        //文件路径
        string fileName = "文件.txt";
        //需要一个stream,这里是直接把数据写入文件了
        using (Stream sm = new FileStream(fileName, FileMode.Create, FileAccess.Read))
        {
              //获取数据
              List<Programmer> pList = DataFactory.BuildProgrammerList();
               //创建二进制序列化器
               BinaryFormatter binformat = new BinaryFormatter();
               //写入文件
               binformat.Serialize(sm, pList);
        }
        //反序列化成对象,从文件中获取数据
        using(Stream sm = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
        {
                //创建二进制序列化器
                BinaryFormatter binformat = new BinaryFormatter();
                //重置流位置
                sm.Position = 0;
                //反序列化
                List<Programmer> pList = (List<Programmer>)binformat.Deserialize(sm);
          }
     }
    /// <summary>
    /// soap序列化器
    /// </summary>
    public static void SoapSerialize()
    {
        //文件路径
        string filePath = "文件.txt";
        //需要一个Stream,这里直接把数据写入文件了
        using (Stream sm = new FileStream(filePath, FileMode.Create, FileAccess.Read))
        {
            //获取数据
            List<Programmer> pList = DataFactory.BuildProgrammerList();
            //创建soap序列化器
            SoapFormatter soapFormt = new SoapFormatter();
            //写入文件
            soapFormt.Serialize(sm, pList);
        }
        //反序列化
        using (Stream sm = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
        {
            //创建soap序列化器
            SoapFormatter soapFormat = new SoapFormatter();
            //序列化成对象
            List<Programmer> pList = (List<Programmer>)soapFormat.Deserialize(sm);
        }
    }
    /// <summary>
    /// XML序列化器
    /// </summary>
    public static void XmlSerialize()
    {
        //文件路径
        string filePath = "";
        using(Stream sm = new FileStream(filePath, FileMode.Create, FileAccess.Read))
        {
            List<Programmer> pList = DataFactory.BuildProgrammerList();
            //创建xml序列化器,需要指定对象的类型
            XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));
            //写入文件
            xmlFormat.Serialize(sm, pList);
        }
        using (Stream sm = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
        {
            //创建xml序列化器
            XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));
            //反序列化成对象
            List<Programmer> pList = (List<Programmer>)xmlFormat.Deserialize(sm);
        }
    }    
  • 相关阅读:
    Tensorflow 2.0 学习资源
    SpagoBI 教程 Lesson 5: Creating a dashboard with BIRT and SpagoBI
    SpagoBI 教程 Lesson 4: BIRT Reports
    SpagoBI 教程 Lesson 3: Highchart Dashboards
    SpagoBI 教程 Lesson 2: OLAP with JPIVOT
    SpagoBI 教程 Lesson 1:Introduction and Installation
    Oracle system表空间满的暂定解决方法
    运算符重载_继承_多态_模版
    成员函数返回的是对象和引用的区别(转)
    String类型_static成员_动态内存分配_拷贝构造函数_const关键字_友元函数与友元类
  • 原文地址:https://www.cnblogs.com/liuqiwang/p/8312824.html
Copyright © 2011-2022 走看看