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

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization.Formatters.Soap;
    using System.Xml.Serialization;
    using System.Text;
    
    namespace SerializableBinarySample
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (FileStream stream = new FileStream("Hello.txt", FileMode.OpenOrCreate))
                {
                    Person p = new Person()
                    {
                        Age = 11,
                        Name = "HuangTao",
                        Sex = "Nan",
                        Sno = "2009"
                    };
                    IFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, p);
                    stream.Close();
                }
                using (FileStream stream = new FileStream("Hello.txt", FileMode.Open))
                {
                    IFormatter formatter = new BinaryFormatter();
                    Person p = (Person)formatter.Deserialize(stream);
                    Console.WriteLine(p.DisplayInfo());
                }
    
                using (FileStream stream = new FileStream("HelloSoap.txt", FileMode.OpenOrCreate))
                {
                    Person p = new Person()
                    {
                        Age = 11,
                        Name = "HuangTao",
                        Sex = "Nan",
                        Sno = "2009"
                    };
                    IFormatter formatter = new SoapFormatter();
                    formatter.Serialize(stream, p);
                    stream.Close();
                }
                using (FileStream stream = new FileStream("HelloSoap.txt", FileMode.Open))
                {
                    IFormatter formatter = new SoapFormatter();
                    Person p = (Person)formatter.Deserialize(stream);
                    Console.WriteLine(p.DisplayInfo());
                    stream.Close();
                }
    
                using (FileStream stream = new FileStream("HelloXml.txt", FileMode.OpenOrCreate))
                {
                    XmlSerializer formatter = new XmlSerializer(typeof(Person));
                    Person p=new Person()
                    {
                        Age = 11,
                        Name = "HuangTao",
                        Sex = "Nan",
                        Sno = "2009"
                    };
                    formatter.Serialize(stream, p);
                    stream.Close();
                }
                using (FileStream stream = new FileStream("HelloXml.txt", FileMode.Open))
                {
                    XmlSerializer formatter = new XmlSerializer(typeof(Person));
                    Person p = (Person)formatter.Deserialize(stream);
                    Console.WriteLine(p.DisplayInfo());
                    stream.Close();
                }
            }
    
        }
    }
  • 相关阅读:
    位移算数符
    蓝桥杯 错误票据(第四届预赛第7题)
    hdu Train Problem I(栈的简单应用)
    ACM YTU 2018 母牛的故事
    ACM YTU 1012 u Calculate e
    C 语言 printf格式控制详解
    ACM YTU 十进制与八进制的转换 (栈和队列) STL栈调用
    ACM HDU 2044 一只小蜜蜂
    ACM YTU 挑战编程 字符串 Problem A: WERTYU
    ACM HDU 1021 Fibonacci Again
  • 原文地址:https://www.cnblogs.com/HelloMyWorld/p/3060219.html
Copyright © 2011-2022 走看看