Using System. IO; using System. runtime. serialization. formatters. binary; using System. xml. serialization; namespace NOAS. publicOpinionMonitor. common {public class SerializeUtility {public enum SerializeType {XML, binary} # region serialization // <summary> // serialization // </summary> // <typeparam name = "T"> </typeparam> /// <param name = "t"> </param> /// <param name = "savePath"> </param> /// <param name = "type"> </ param> Public static void Serialize <T> (T t, string savePath, SerializeType type) {switch (type) {case SerializeType. XML: SerializeToXML <T> (t, savePath); break; case SerializeType. binary: SerializeToBinary <T> (t, savePath); break; default: break ;}} /// <summary> /// serialize the file to an XML Format File /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "t"> </param> // <param name = "savePath"> </param> private s Tatic void SerializeToXML <T> (T t, string savePath) {if (! Directory. getParent (savePath ). exists) Directory. getParent (savePath ). create (); XmlSerializer ser = new XmlSerializer (t. getType (); if (File. exists (savePath) {File. delete (savePath);} using (FileStream stream = new FileStream (savePath, FileMode. openOrCreate, FileAccess. readWrite, FileShare. readWrite) {ser. serialize (stream, t) ;}/// <summary> // Serialize the file to Binary format /// </summary> /// <typeparam nam E = "T"> </typeparam> // <param name = "t"> </param> // <param name = "savePath"> </param> private static void SerializeToBinary <T> (T t, string savePath) {if (! Directory. getParent (savePath ). exists) Directory. getParent (savePath ). create (); BinaryFormatter serilazer = new BinaryFormatter (); using (FileStream stream = new FileStream (savePath, FileMode. create, FileAccess. readWrite, FileShare. readWrite) {serilazer. serialize (stream, t) ;}# endregion # region deserialization public static T DeSerialize <T> (string readPath, SerializeType type) {switch (type) {case Serial IzeType. XML: return DeSerializeXML <T> (readPath); case SerializeType. binary: return DeSerializeBinary <T> (readPath); default: return default (T );}} /// <summary> /// deserialize the XML file /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "savePath"> </param> // <returns> </returns> private static T DeSerializeXML <T> (string readPath) {if (! File. exists (readPath) {return default (T);} XmlSerializer ser = new XmlSerializer (typeof (T); using (FileStream stream = new FileStream (readPath, FileMode. openOrCreate, FileAccess. readWrite, FileShare. readWrite) {return (T) ser. deserialize (stream );}} /// <summary> /// deserialize the Binary file /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "savePath"> </param> // <returns> </returns> private static T DeSerializeBinary <T> (string readPath) {BinaryFormatter serilazer = new BinaryFormatter (); using (FileStream stream = new FileStream (readPath, FileMode. openOrCreate, FileAccess. readWrite, FileShare. readWrite) {return (T) serilazer. deserialize (stream) ;}# endregion }}
This method is used to directly read files.