zoukankan      html  css  js  c++  java
  • 序列化与反序列化Serialize&Deserialize

    序列化是指一个对象的实例可以被保存,保存成一个二进制串,当然,一旦被保存成二进制串,那么也可以保存成文本串了。
    比如,一个计数器,数值为2,我们可以用字符串“2”表示。
    如果有个对象,叫做connter,当前值为2,那么可以序列化成“2”,反向的,也可以从“2”得到值为2的计数器实例。
    这样,关机时序列化它,开机时反序列化它,每次开机都是延续的。不会都是从头开始。

    序列化概念的提出和实现,可以使我们的应用程序的设置信息保存和读取更加方便。

    序列化有很多好处,比如,在一台机器上产生一个实例,初始化完毕,然后可以序列化,通过网络传送到另一台机器,然后反序列化,得到对象实例,之后再执行某些业务逻辑,得到结果,再序列化,返回第一台机器,第一台机器得到对象实例,得到结果。

    一、二进制序列化与反序列化

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace Serialization
    {
        [Serializable]
        public class A
        {
            public string Name { get; set; }
            public string Id { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                var a=new A {Name = "mk", Id = "121"};
                var formater = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                Stream stream =new FileStream("UserFileInfo.bin",FileMode.Create,FileAccess.Write,FileShare.None);
                formater.Serialize(stream ,a);
                stream.Close();
    
    
                stream = new FileStream("UserFileInfo.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
                var b=(A)formater.Deserialize(stream);
                stream.Close();
                Console.WriteLine("name{0}",b.Name);
                Console.WriteLine("Id{0}",b.Id);
                Console.Read();
            }
    
        }
    }

    二、XML的序列化与反序列化

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    
    namespace XMLSerialization
    {
        [Serializable]
        public class A
        {
            public string Name { get; set; }
            public string Id { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                var a = new A { Name = "yq", Id = "121" };
                //var formater = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                //Stream stream = new FileStream("UserFileInfo.bin", FileMode.Create, FileAccess.Write, FileShare.None);
                XmlSerializer mySerializer=new XmlSerializer(typeof(A));
                Stream stream = new FileStream("UserFileInfo.xml", FileMode.Create, FileAccess.Write, FileShare.None);
                mySerializer.Serialize(stream, a);
                stream.Close();
    
    
                stream = new FileStream("UserFileInfo.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
                var b = (A)mySerializer.Deserialize(stream);
                stream.Close();
                Console.WriteLine("name{0}", b.Name);
                Console.WriteLine("Id{0}", b.Id);
                Console.Read();
            }
    
        }
    }
    UserFileInfo.xml返回内容
    <?xml version="1.0"?>
    <A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Name>yq</Name>
      <Id>121</Id>
    </A>
    

      



         

  • 相关阅读:
    PHP vscode+XDebug 远程断点调试服务器上的代码
    Wordpress 为用户或角色 role 添加 capabilities(权限)
    Wordpress 后台文章编辑区添加模板选择功能
    CentOS 7 编译安装最新版git
    WordPress 通过文章 URL 获取文章 ID
    Web 安全问题 rel="noopener nofollw"
    Wordpress 通过 post id 获取文章 url
    git放弃修改&放弃增加文件
    Wordpress 作者模板页中的自定义帖子类型分页问题
    PHP 删除 url 中的 query string
  • 原文地址:https://www.cnblogs.com/iammackong/p/3255098.html
Copyright © 2011-2022 走看看