标签:style public bsp get 反序列化 mode new static file
1.UserInfo对象类
[Serializable] public class UserInfo { public string Name { get; set; } public int Age { get; set; } }
2.Xml方式序列化和反序列化
public class XmlSerialization { public static void XmlSerialize(UserInfo user) { XmlSerializer serializer = new XmlSerializer(typeof(UserInfo)); StreamWriter sw = new StreamWriter("UserInfo.xml"); serializer.Serialize(sw, user); sw.Close(); } public static UserInfo XmlDeserialize() { XmlSerializer serializer = new XmlSerializer(typeof(UserInfo)); FileStream fs = new FileStream("UserInfo.xml", FileMode.Open); UserInfo user = serializer.Deserialize(fs) as UserInfo; fs.Close(); return user; } }
3.Binary方式序列化和反序列化
public class RuntimeSerialization { public static void BinarySerialize(UserInfo user) { FileStream fs = new FileStream(@"SerializeFile\UserInfo.bin", FileMode.Append); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, user); fs.Close(); } public static UserInfo BinaryDeserialize() { FileStream fs = new FileStream(@"SerializeFile\UserInfo.bin", FileMode.Open, FileAccess.Read, FileShare.Read); BinaryFormatter formatter = new BinaryFormatter(); UserInfo user = formatter.Deserialize(fs) as UserInfo; fs.Close(); return user; } }
标签:style public bsp get 反序列化 mode new static file
原文地址:http://www.cnblogs.com/DaphneOdera/p/7113334.html