如何保存/恢复可序列化的对象到/从文件?

我有一个对象的列表,我需要保存在我的电脑的某个地方。 我已经阅读了一些论坛,我知道对象必须是可Serializable 。 但是如果我能得到一个例子,这将是很好的。 例如,如果我有以下内容:

 [Serializable] public class SomeClass { public string someProperty { get; set; } } SomeClass object1 = new SomeClass { someProperty = "someString" }; 

但是,我怎么能存储object1在我的电脑的某个地方,以后检索?

您可以使用以下内容:

  /// <summary> /// Serializes an object. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="serializableObject"></param> /// <param name="fileName"></param> public void SerializeObject<T>(T serializableObject, string fileName) { if (serializableObject == null) { return; } try { XmlDocument xmlDocument = new XmlDocument(); XmlSerializer serializer = new XmlSerializer(serializableObject.GetType()); using (MemoryStream stream = new MemoryStream()) { serializer.Serialize(stream, serializableObject); stream.Position = 0; xmlDocument.Load(stream); xmlDocument.Save(fileName); stream.Close(); } } catch (Exception ex) { //Log exception here } } /// <summary> /// Deserializes an xml file into an object list /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName"></param> /// <returns></returns> public T DeSerializeObject<T>(string fileName) { if (string.IsNullOrEmpty(fileName)) { return default(T); } T objectOut = default(T); try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(fileName); string xmlString = xmlDocument.OuterXml; using (StringReader read = new StringReader(xmlString)) { Type outType = typeof(T); XmlSerializer serializer = new XmlSerializer(outType); using (XmlReader reader = new XmlTextReader(read)) { objectOut = (T)serializer.Deserialize(reader); reader.Close(); } read.Close(); } } catch (Exception ex) { //Log exception here } return objectOut; } 

我刚刚写了一篇关于将对象的数据保存到Binary,XML或Json的博文 。 你是正确的,你必须用[Serializable]属性修饰你的类,但只有在你使用二进制序列化的时候。 您可能更喜欢使用XML或Json序列化。 这里是以各种格式做的function。 看到我的博客文章了解更多细节。

二进制

 /// <summary> /// Writes the given object instance to a binary file. /// <para>Object type (and all child types) must be decorated with the [Serializable] attribute.</para> /// <para>To prevent a variable from being serialized, decorate it with the [NonSerialized] attribute; cannot be applied to properties.</para> /// </summary> /// <typeparam name="T">The type of object being written to the XML file.</typeparam> /// <param name="filePath">The file path to write the object instance to.</param> /// <param name="objectToWrite">The object instance to write to the XML file.</param> /// <param name="append">If false the file will be overwritten if it already exists. If true the contents will be appended to the file.</param> public static void WriteToBinaryFile<T>(string filePath, T objectToWrite, bool append = false) { using (Stream stream = File.Open(filePath, append ? FileMode.Append : FileMode.Create)) { var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); binaryFormatter.Serialize(stream, objectToWrite); } } /// <summary> /// Reads an object instance from a binary file. /// </summary> /// <typeparam name="T">The type of object to read from the XML.</typeparam> /// <param name="filePath">The file path to read the object instance from.</param> /// <returns>Returns a new instance of the object read from the binary file.</returns> public static T ReadFromBinaryFile<T>(string filePath) { using (Stream stream = File.Open(filePath, FileMode.Open)) { var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); return (T)binaryFormatter.Deserialize(stream); } } 

XML

需要将System.Xml程序集包含在您的项目中。

 /// <summary> /// Writes the given object instance to an XML file. /// <para>Only Public properties and variables will be written to the file. These can be any type though, even other classes.</para> /// <para>If there are public properties/variables that you do not want written to the file, decorate them with the [XmlIgnore] attribute.</para> /// <para>Object type must have a parameterless constructor.</para> /// </summary> /// <typeparam name="T">The type of object being written to the file.</typeparam> /// <param name="filePath">The file path to write the object instance to.</param> /// <param name="objectToWrite">The object instance to write to the file.</param> /// <param name="append">If false the file will be overwritten if it already exists. If true the contents will be appended to the file.</param> public static void WriteToXmlFile<T>(string filePath, T objectToWrite, bool append = false) where T : new() { TextWriter writer = null; try { var serializer = new XmlSerializer(typeof(T)); writer = new StreamWriter(filePath, append); serializer.Serialize(writer, objectToWrite); } finally { if (writer != null) writer.Close(); } } /// <summary> /// Reads an object instance from an XML file. /// <para>Object type must have a parameterless constructor.</para> /// </summary> /// <typeparam name="T">The type of object to read from the file.</typeparam> /// <param name="filePath">The file path to read the object instance from.</param> /// <returns>Returns a new instance of the object read from the XML file.</returns> public static T ReadFromXmlFile<T>(string filePath) where T : new() { TextReader reader = null; try { var serializer = new XmlSerializer(typeof(T)); reader = new StreamReader(filePath); return (T)serializer.Deserialize(reader); } finally { if (reader != null) reader.Close(); } } 

JSON

您必须包含对Newtonsoft.Json程序集的引用,该程序集可以从Json.NET NuGet程序包中获取 。

 /// <summary> /// Writes the given object instance to a Json file. /// <para>Object type must have a parameterless constructor.</para> /// <para>Only Public properties and variables will be written to the file. These can be any type though, even other classes.</para> /// <para>If there are public properties/variables that you do not want written to the file, decorate them with the [JsonIgnore] attribute.</para> /// </summary> /// <typeparam name="T">The type of object being written to the file.</typeparam> /// <param name="filePath">The file path to write the object instance to.</param> /// <param name="objectToWrite">The object instance to write to the file.</param> /// <param name="append">If false the file will be overwritten if it already exists. If true the contents will be appended to the file.</param> public static void WriteToJsonFile<T>(string filePath, T objectToWrite, bool append = false) where T : new() { TextWriter writer = null; try { var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite); writer = new StreamWriter(filePath, append); writer.Write(contentsToWriteToFile); } finally { if (writer != null) writer.Close(); } } /// <summary> /// Reads an object instance from an Json file. /// <para>Object type must have a parameterless constructor.</para> /// </summary> /// <typeparam name="T">The type of object to read from the file.</typeparam> /// <param name="filePath">The file path to read the object instance from.</param> /// <returns>Returns a new instance of the object read from the Json file.</returns> public static T ReadFromJsonFile<T>(string filePath) where T : new() { TextReader reader = null; try { reader = new StreamReader(filePath); var fileContents = reader.ReadToEnd(); return JsonConvert.DeserializeObject<T>(fileContents); } finally { if (reader != null) reader.Close(); } } 

 // Write the contents of the variable someClass to a file. WriteToBinaryFile<SomeClass>("C:\someClass.txt", object1); // Read the file contents back into a variable. SomeClass object1= ReadFromBinaryFile<SomeClass>("C:\someClass.txt"); 

你将需要序列化到某些东西:也就是说,select二进制或xml(对于默认序列化程序)或编写自定义序列化代码来序列化为其他文本forms。

一旦你select了,你的序列化将(通常)调用正在写入某种文件的stream。

因此,如果使用XML序列化,使用您的代码:

 var path = @"C:\Test\myserializationtest.xml"; using(FileStream fs = new FileStream(path, FileMode.Create)) { XmlSerializer xSer = new XmlSerializer(typeof(SomeClass)); xSer.Serialize(fs, serializableObject); } 

然后,反序列化:

 using(FileStream fs = new FileStream(path, FileMode.Open)) //double check that... { XmlSerializer _xSer = new XmlSerializer(typeof(SomeClass)); var myObject = _xSer.Deserialize(fs); } 

注意:此代码尚未编译,更不用说运行 – 可能会出现一些错误。 而且,这个假设完全是开箱即用的序列化/反序列化。 如果你需要自定义行为,你需要做额外的工作。

1.从文件恢复对象

从这里你可以反序列化两个文件中的一个对象。

解决scheme1:将文件读入string并将JSON反序列化为types

 string json = File.ReadAllText(@"c:\myObj.json"); MyObject myObj = JsonConvert.DeserializeObject<MyObject>(json); 

解决scheme2:直接从文件反序列化JSON

 using (StreamReader file = File.OpenText(@"c:\myObj.json")) { JsonSerializer serializer = new JsonSerializer(); MyObject myObj2 = (MyObject)serializer.Deserialize(file, typeof(MyObject)); } 

2.将对象保存到文件

从这里你可以序列化一个对象到两个文件。

解决scheme1:将JSON序列化为string,然后将string写入文件

 string json = JsonConvert.SerializeObject(myObj); File.WriteAllText(@"c:\myObj.json", json); 

解决scheme2:将JSON直接序列化到文件

 using (StreamWriter file = File.CreateText(@"c:\myObj.json")) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(file, myObj); } 

3.额外

您可以通过以下命令从NuGet下载Newtonsoft.Json

 Install-Package Newtonsoft.Json