八、存档服务——1、类序列化为xml和反序列化
2021-09-26 本文已影响0人
GameObjectLgy
序列化
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "一些字符串";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
反序列化
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile。bin", FileMode.Open,
FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject) formatter.Deserialize(fromStream);
stream.Close();
```C#
##### 序列化标签
[Serializable] 对某个类进行序列化
[NonSerialized] 对某个字段不进行序列化
##### 读取xml文件