zoukankan      html  css  js  c++  java
  • 序列化和反序列化 echo

    将一个对象序列化成二进制、XML文件,以及反序列化对象。

    一、将要被序列化的对象(Object)

    using System;
    
    namespace SerializationLibrary
    {
        /// <summary>
        /// MyObject实体类,用来作序列化测试
        /// </summary>
        [Serializable]
        public class MyObject
        {
            private int age;
            /// <summary>
            /// 年龄
            /// </summary>
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
            private string gender;
            /// <summary>
            /// 性别
            /// </summary>
            public string Gender
            {
                get { return gender; }
                set { gender = value; }
            }
            private string name;
            /// <summary>
            /// 姓名
            /// </summary>
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private string address;
            /// <summary>
            /// 地址
            /// </summary>
            public string Address
            {
                get { return address; }
                set { address = value; }
            }
        }
    }

    二、处理类:

    using SerializationLibrary;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Xml.Serialization;
    using System.IO;
    using System.Windows.Forms;
    
    namespace Serialization
    {
        /// <summary>
        /// 可操作序列化的类
        /// </summary>
        public static class SerializableHandler
        {
            /// <summary>
            /// 将被序列化的对象
            /// </summary>
            static MyObject Obj = new MyObject
            {
                Name = "Nicholas Nie",
                Age = 23,
                Gender = "Man",
                Address = "Beijing"
            };
            /// <summary>
            /// 序列化到二进制
            /// </summary>
            public static void SerializationToBinary()
            {
                IFormatter Formatter = new BinaryFormatter();
                Stream Stream = new FileStream(@"E:\WindowsProgram\Serialization\MyObj.bin", FileMode.Create, FileAccess.Write, FileShare.None);
                Formatter.Serialize(Stream, Obj);
                Stream.Close();
                FileInfo FileInfo = new FileInfo(@"E:\WindowsProgram\Serialization\MyObj.bin");
                if (FileInfo.Exists)
                {
                    MessageBox.Show("序列化操作成功!\n" + "对象被序列化到" + FileInfo.FullName, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("序列化操作失败!", "出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
    
            /// <summary>
            /// 根据二进制反序列化
            /// </summary>
            /// <returns></returns> 
            public static MyObject DeserializationByBinary()
            {
                FileInfo FileInfo = new FileInfo(@"E:\WindowsProgram\Serialization\MyObj.bin");
                if (FileInfo.Exists)
                {
                    IFormatter Formatter = new BinaryFormatter();
                    Stream Stream = new FileStream(@"E:\WindowsProgram\Serialization\MyObj.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
                    MyObject RestoreObject = (MyObject)Formatter.Deserialize(Stream);
                    Stream.Close();
                    MessageBox.Show("反序列化操作成功!\n" +
                        "对象类型:" + RestoreObject.ToString() + 
                        " \nName:" + RestoreObject.Name + 
                        "\nAge:" + RestoreObject.Age + 
                        "\nGender:"+RestoreObject.Gender + 
                        "\nAddress:"+RestoreObject.Address, 
                        "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return RestoreObject;
                }
                else
                {
                    MessageBox.Show("反序列化操作失败!\n" + "不存在可反序列化的文件!\n" + "路径" + FileInfo.FullName, "出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return null;
                }
            }
    
            /// <summary>
            /// 序列化到Xml
            /// </summary>
            public static void SerializationToXml()
            {
                XmlSerializer XmlSer = new XmlSerializer(typeof(MyObject));
                StreamWriter SW = new StreamWriter(@"E:\WindowsProgram\Serialization\MyObj.xml");
                XmlSer.Serialize(SW, Obj);
                SW.Close();
                FileInfo Fileinfo = new FileInfo(@"E:\WindowsProgram\Serialization\MyObj.xml");
                if (Fileinfo.Exists)
                {
                    MessageBox.Show("序列化操作成功!\n" + "对象被序列化到" + Fileinfo.FullName, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("序列化操作失败!", "出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
    
            /// <summary>
            /// 根据Xml反序列化
            /// </summary>
            /// <returns></returns>
            public static MyObject DeserializationByXml()
            {
                XmlSerializer XmlSer = null;
                FileStream FileStream = null;
                FileInfo FileInfo = new FileInfo(@"E:\WindowsProgram\Serialization\MyObj.xml");
                if (FileInfo.Exists)
                {
                    XmlSer = new XmlSerializer(typeof(MyObject));
                    FileStream = new FileStream(@"E:\WindowsProgram\Serialization\MyObj.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
                    MyObject RestoreObject = (MyObject)XmlSer.Deserialize(FileStream);
                    FileStream.Close();
                    MessageBox.Show("反序列化操作成功!\n" +
                        "对象类型:" + RestoreObject.ToString() +
                        " \nName:" + RestoreObject.Name +
                        "\nAge:" + RestoreObject.Age +
                        "\nGender:" + RestoreObject.Gender +
                        "\nAddress:" + RestoreObject.Address,
                        "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return RestoreObject;
                }
                else
                {
                    MessageBox.Show("序列化操作失败!", "出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return null;
                }
            }
        }
    }
    
    
  • 相关阅读:
    【Quartz】1、Quartz使用说明
    【Servlet】1、Servlet监听器及相关接口
    【IDEA&&Eclipse】5、IntelliJ IDEA常见配置
    Chris Richardson微服务实战系列
    Traefik Kubernetes 初试
    用友iuap云运维平台支持基于K8s的微服务架构
    DCOS中监控和弹性伸缩方案经验
    使用微服务架构改造遗留系统
    kubernetes中port、target port、node port的对比分析,以及kube-proxy代理
    基于prometheus监控k8s集群
  • 原文地址:https://www.cnblogs.com/mangonic/p/1789322.html
Copyright © 2011-2022 走看看