zoukankan      html  css  js  c++  java
  • 对象序列化笔记

     

    using System;

    using System.Xml.Serialization; //Xml序列化时引入

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Web.Script.Serialization; //json序列化时引入

    using System.IO;

    using System.Runtime.Serialization.Formatters.Binary; //二进制序列化时引入

     

     

    namespace ConsoleApplication6

    {

    class Program

    {

    static void Main(string[] args)

    {

    Person p1 = new Person();

    p1.Name = "池少舫";

    p1.Age = 10;

    p1.Email = "csf@chi.com";

     

    //json序列化

    //JavaScriptSerializer jss = new JavaScriptSerializer();

    //string str = jss.Serialize(p1);

    //for (int i = 0; i < str.Length; i++)

    //{

    // File.WriteAllText("person.json", str);

    //}

     

    //xml序列化

    //XmlSerializer xmls = new XmlSerializer(typeof(Person));

    //FileStream fs = new FileStream("person.xml", FileMode.Create);

    //xmls.Serialize(fs, p1);

     

    //二进制序列化

    BinaryFormatter bf = new BinaryFormatter();

    using (FileStream fs = new FileStream("person.bin",FileMode.Create))

    {

    bf.Serialize(fs, p1);

    }

     

    Console.WriteLine("ok");

    Console.ReadKey();

     

    }

    }

     

    [Serializable]

    public class Person

    {

    public string Name;

    public int Age;

    public string Email;

    }

    }

  • 相关阅读:
    MVC初体验-过滤器(10)
    MVC阶段复习(一)
    搭建自己的视频文件网站
    答辩系统bug修改记录
    linux的pvtrace环境配置
    Linux使用期间命令积累
    php项目第三季
    分布式作业
    php第二季
    php错误记录
  • 原文地址:https://www.cnblogs.com/HarryChis/p/13634252.html
Copyright © 2011-2022 走看看