zoukankan      html  css  js  c++  java
  • C# 序列化(Binary、Xml、Soap)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Xml.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization.Formatters.Soap;
    
    namespace Serialization
    {
        class Program
        {
            static void Main(string[] args)
            {
                Student stu = new Student("张三", 30, "登山");
                
                ////二进制序列化
                //BinaryFormatter bf = new BinaryFormatter();
                //FileStream fs = new FileStream(@"D:stu.ss", FileMode.Create);
                //bf.Serialize(fs, stu);
                //fs.Close();
    
                ////二进制反序列化
                //BinaryFormatter bf = new BinaryFormatter();
                //FileStream fs = new FileStream(@"D:stu.ss", FileMode.Open);
                //Student student = (Student)bf.Deserialize(fs);
                //student.method();
    
                ////xml序列化
                //XmlSerializer xml = new XmlSerializer(typeof(Student));
                //FileStream fs = new FileStream(@"D:stu.xml",FileMode.Create);
                //xml.Serialize(fs, stu);
                //fs.Close();
    
                ////xml反序列化
                //XmlSerializer xml = new XmlSerializer(typeof(Student));
                //FileStream fs = new FileStream(@"D:stu.xml", FileMode.Open);
                //Student student = (Student)xml.Deserialize(fs);
                //student.method();
    
                ////soap序列化
                //SoapFormatter soap = new SoapFormatter();
                //FileStream fs = new FileStream(@"D:stu.soap", FileMode.Create);
                //soap.Serialize(fs, stu);
                //fs.Close();
    
                //soap反序列化
                SoapFormatter soap = new SoapFormatter();
                FileStream fs = new FileStream(@"D:stu.soap", FileMode.Open);
                Student student = (Student)soap.Deserialize(fs);
                student.method();
    
                
            }
        }
    
    
        [Serializable]
        public class Person
        {
            public Person()
            {
    
            }
    
            public Person(string name, int age)
            {
                this.Name = name;
                this.Age = age;
            }
    
            public string Name
            {
                get;
                set;
            }
            public int Age
            {
                get;
                set;
            }
        }
    
        [Serializable]
        public class Student : Person
        {
            public Student()
            {
    
            }
    
            public Student(string name, int age, string hobby)
                : base(name, age)
            {
                this.Hobby = hobby;
            }
    
            public string Hobby
            {
                get;
                set;
            }
    
            public void method()
            {
                Console.WriteLine("大家好,我叫{0},今年{1}岁,我喜欢{2}", this.Name, this.Age, this.Hobby);
                //Console.ReadLine();
            }
        }
    }
    View Code
  • 相关阅读:
    什么是multipart/form-data请求
    jquery.fancybox
    laravel上传图片报错
    php框架安装
    ExtJS入门教程02,form也可以很优雅
    ExtJS入门教程01,Window如此简单,你怎能不会?
    Ext.Net学习笔记24:在ASP.NET MVC中使用Ext.Net
    Ext.Net学习笔记23:Ext.Net TabPanel用法详解
    Ext.Net学习笔记22:Ext.Net Tree 用法详解
    Ext.Net学习笔记21:Ext.Net FormPanel 字段验证(validation)
  • 原文地址:https://www.cnblogs.com/ZJ199012/p/4151341.html
Copyright © 2011-2022 走看看