public static void BinarySerialize(UserInfo user) { FileStream fs = new FileStream("MySerialze.bin", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); //ִ�ж��������л� formatter.Serialize(fs, user); fs.Close(); }
//Main_9_4_7 public static void Main_9_4_7() { UserInfo user = new UserInfo("С��", 26, true); //ִ�����л� BinarySerialize(user); //ִ�з����л� UserInfo user2 = BinaryDeserialize(); Console.WriteLine("Name:{0}\nAge:{1}\nVip:{2}", user2.Name, user2.Age, user2.IsVIP); }
//Main_9_4_8 public static void Main_9_4_8() { UserInfo user = new UserInfo("С��", 27, false); XmlSerializer serialsizer = new XmlSerializer(typeof(UserInfo)); //ִ�����л����� StreamWriter sw = new StreamWriter("MySerialze.xml"); serialsizer.Serialize(sw, user); sw.Close(); //ִ�з����л����� FileStream fs = new FileStream("MySerialze.xml", FileMode.Open); UserInfo user2 = serialsizer.Deserialize(fs) as UserInfo; Console.WriteLine("Name:{0}\nAge:{1}\nVip:{2}\nDes:{3}", user2.Name, user2.Age, user2.IsVIP, user2.ShowDes()); }