对象序列化笔记
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;
}
}