/**/
/// <summary>
/// 泛型方法,将当对象序列化保存到XML文件
/// </summary>
/// <param name="对象">对象</param>
/// <param name="存储路径">文件保存的位置</param>
public static void XML序列化对象 < 类型 > (类型 对象, string 存储路径)
... {
XmlSerializer XS = new XmlSerializer(typeof(类型));
Stream stream = new FileStream(存储路径, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XS.Serialize(stream, 对象);
stream.Close();
}
/**/ /// <summary>
/// 泛型方法,从序列化XML文件中读取当前类的实例
/// </summary>
/// <param name="存储路径">文件保存的位置</param>
/// <returns>对象</returns>
public static 类型 XML反序列化对象 < 类型 > ( string 存储路径)
... {
XmlSerializer XS = new XmlSerializer(typeof(类型));
Stream stream = new FileStream(存储路径, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlTextReader reader = new XmlTextReader(stream);
reader.Normalization = false;
类型 OBJ = (类型)XS.Deserialize(reader);
stream.Close();
return OBJ;
}
/**/ /// <summary>
/// 将指定的对象序列化为二进制文件,如果该对象不支持序列化,将会引发异常
/// </summary>
/// <param name="对象">要进行序列化的对象</param>
/// <param name="路径">保存的文件路径</param>
public static void 序列化对象( object 对象, string 路径)
... {
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(路径, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, 对象);
stream.Close();
}
/**/ /// <summary>
/// 通过序列化的二进制文件反序列化对象
/// </summary>
/// <param name="路径">此前保存序列化文件的路径</param>
/// <returns>对象</returns>
public static object 反序列化对象( string 路径)
... {
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(路径, FileMode.Open, FileAccess.Read, FileShare.Read);
object OBJ = formatter.Deserialize(stream);
stream.Close();
return OBJ;
}
/// 泛型方法,将当对象序列化保存到XML文件
/// </summary>
/// <param name="对象">对象</param>
/// <param name="存储路径">文件保存的位置</param>
public static void XML序列化对象 < 类型 > (类型 对象, string 存储路径)
... {
XmlSerializer XS = new XmlSerializer(typeof(类型));
Stream stream = new FileStream(存储路径, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XS.Serialize(stream, 对象);
stream.Close();
}
/**/ /// <summary>
/// 泛型方法,从序列化XML文件中读取当前类的实例
/// </summary>
/// <param name="存储路径">文件保存的位置</param>
/// <returns>对象</returns>
public static 类型 XML反序列化对象 < 类型 > ( string 存储路径)
... {
XmlSerializer XS = new XmlSerializer(typeof(类型));
Stream stream = new FileStream(存储路径, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlTextReader reader = new XmlTextReader(stream);
reader.Normalization = false;
类型 OBJ = (类型)XS.Deserialize(reader);
stream.Close();
return OBJ;
}
/**/ /// <summary>
/// 将指定的对象序列化为二进制文件,如果该对象不支持序列化,将会引发异常
/// </summary>
/// <param name="对象">要进行序列化的对象</param>
/// <param name="路径">保存的文件路径</param>
public static void 序列化对象( object 对象, string 路径)
... {
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(路径, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, 对象);
stream.Close();
}
/**/ /// <summary>
/// 通过序列化的二进制文件反序列化对象
/// </summary>
/// <param name="路径">此前保存序列化文件的路径</param>
/// <returns>对象</returns>
public static object 反序列化对象( string 路径)
... {
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(路径, FileMode.Open, FileAccess.Read, FileShare.Read);
object OBJ = formatter.Deserialize(stream);
stream.Close();
return OBJ;
}