Saving And Loading A C# Object’s Data To An Xml, Json, Or Binary File
I love creating tools, particularly ones for myself and other developers to use. A common situation that I run into is needing to save the user’s settings to a file so that I can load them up the next time the tool is ran. I find that the easiest way to accomplish this is to create a Settings class to hold all of the user’s settings, and then use serialization to save and load the class instance to/from a file. I mention a Settings class here, but you can use this technique to save any object (or list of objects) to a file.
There are tons of different formats that you may want to save your object instances as, but the big three are Binary, XML, and Json. Each of these formats have their pros and cons, which I won’t go into. Below I present functions that can be used to save and load any object instance to / from a file, as well as the different aspects to be aware of when using each method.
The follow code (without examples of how to use it) is also available here, and can be used directly from my NuGet Package.
Writing and Reading an object to / from a Binary file
UPDATE: BinaryFormatter is being decommissioned due to security vulnerabilities it introduces, so it should no longer be used. Read more on the MS docs here
- Writes and reads ALL object properties and variables to / from the file (i.e. public, protected, internal, and private).
- The data saved to the file is not human readable, and thus cannot be edited outside of your application.
- Have to decorate class (and all classes that it contains) with a [Serializable] attribute.
- Use the [NonSerialized] attribute to exclude a variable from being written to the file; there is no way to prevent an auto-property from being serialized besides making it use a backing variable and putting the [NonSerialized] attribute on that.
/// <summary>
/// Functions for performing common binary Serialization operations.
/// <para>All properties and variables will be serialized.</para>
/// <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>
public static class BinarySerialization
{
/// <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);
}
}
}
And here is an example of how to use it:
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age = 20;
public Address HomeAddress { get; set;}
private string _thisWillGetWrittenToTheFileToo = "even though it is a private variable.";
[NonSerialized]
public string ThisWillNotBeWrittenToTheFile = "because of the [NonSerialized] attribute.";
}
[Serializable]
public class Address
{
public string StreetAddress { get; set; }
public string City { get; set; }
}
// And then in some function.
Person person = new Person() { Name = "Dan", Age = 30; HomeAddress = new Address() { StreetAddress = "123 My St", City = "Regina" }};
List<Person> people = GetListOfPeople();
BinarySerialization.WriteToBinaryFile<Person>("C:\person.bin", person);
BinarySerialization.WriteToBinaryFile<List<People>>("C:\people.bin", people);
// Then in some other function.
Person person = BinarySerialization.ReadFromBinaryFile<Person>("C:\person.bin");
List<Person> people = BinarySerialization.ReadFromBinaryFile<List<Person>>("C:\people.bin");
Writing and Reading an object to / from an XML file (Using System.Xml.Serialization.XmlSerializer in the System.Xml assembly)
- Only writes and reads the Public properties and variables to / from the file.
- Classes to be serialized must contain a public parameterless constructor.
- The data saved to the file is human readable, so it can easily be edited outside of your application.
- Use the [XmlIgnore] attribute to exclude a public property or variable from being written to the file.
/// <summary>
/// Functions for performing common XML Serialization operations.
/// <para>Only public properties and variables will be serialized.</para>
/// <para>Use the [XmlIgnore] attribute to prevent a property/variable from being serialized.</para>
/// <para>Object to be serialized must have a parameterless constructor.</para>
/// </summary>
public static class XmlSerialization
{
/// <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();
}
}
}
And here is an example of how to use it:
public class Person
{
public string Name { get; set; }
public int Age = 20;
public Address HomeAddress { get; set;}
private string _thisWillNotGetWrittenToTheFile = "because it is not public.";
[XmlIgnore]
public string ThisWillNotBeWrittenToTheFile = "because of the [XmlIgnore] attribute.";
}
public class Address
{
public string StreetAddress { get; set; }
public string City { get; set; }
}
// And then in some function.
Person person = new Person() { Name = "Dan", Age = 30; HomeAddress = new Address() { StreetAddress = "123 My St", City = "Regina" }};
List<Person> people = GetListOfPeople();
XmlSerialization.WriteToXmlFile<Person>("C:\person.txt", person);
XmlSerialization.WriteToXmlFile<List<People>>("C:\people.txt", people);
// Then in some other function.
Person person = XmlSerialization.ReadFromXmlFile<Person>("C:\person.txt");
List<Person> people = XmlSerialization.ReadFromXmlFile<List<Person>>("C:\people.txt");
Writing and Reading an object to / from a Json file (using the Newtonsoft.Json assembly in the Json.NET NuGet package)
UPDATE: As an alternative to Newtonsoft.Json, the
System.Text.Json
class is included out of the box starting in .NET Core 3. If it is not available in your .NET version, you can use the System.Text.Json NuGet package. See the Json.NET to System.Text.Json migration guide for more information.
- Only writes and reads the Public properties and variables to / from the file.
- Classes to be serialized must contain a public parameterless constructor.
- The data saved to the file is human readable, so it can easily be edited outside of your application.
- Use the [JsonIgnore] attribute to exclude a public property or variable from being written to the file.
/// <summary>
/// Functions for performing common Json Serialization operations.
/// <para>Requires the Newtonsoft.Json assembly (Json.Net package in NuGet Gallery) to be referenced in your project.</para>
/// <para>Only public properties and variables will be serialized.</para>
/// <para>Use the [JsonIgnore] attribute to ignore specific public properties or variables.</para>
/// <para>Object to be serialized must have a parameterless constructor.</para>
/// </summary>
public static class JsonSerialization
{
/// <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 = Newtonsoft.Json.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 Newtonsoft.Json.JsonConvert.DeserializeObject<T>(fileContents);
}
finally
{
if (reader != null)
reader.Close();
}
}
}
And here is an example of how to use it:
public class Person
{
public string Name { get; set; }
public int Age = 20;
public Address HomeAddress { get; set;}
private string _thisWillNotGetWrittenToTheFile = "because it is not public.";
[JsonIgnore]
public string ThisWillNotBeWrittenToTheFile = "because of the [JsonIgnore] attribute.";
}
public class Address
{
public string StreetAddress { get; set; }
public string City { get; set; }
}
// And then in some function.
Person person = new Person() { Name = "Dan", Age = 30; HomeAddress = new Address() { StreetAddress = "123 My St", City = "Regina" }};
List<Person> people = GetListOfPeople();
JsonSerialization.WriteToJsonFile<Person>("C:\person.txt", person);
JsonSerialization.WriteToJsonFile<List<People>>("C:\people.txt", people);
// Then in some other function.
Person person = JsonSerialization.ReadFromJsonFile<Person>("C:\person.txt");
List<Person> people = JsonSerialization.ReadFromJsonFile<List<Person>>("C:\people.txt");
As you can see, the Json example is almost identical to the Xml example, with the exception of using the [JsonIgnore] attribute instead of [XmlIgnore].
Writing and Reading an object to / from a Json file (using the JavaScriptSerializer in the System.Web.Extensions assembly)
There are many Json serialization libraries out there. I mentioned the Newtonsoft.Json one because it is very popular, and I am also mentioning this JavaScriptSerializer one because it is built into the .Net framework. The catch with this one though is that it requires the Full .Net 4.0 framework, not just the .Net Framework 4.0 Client Profile.
The caveats to be aware of are the same between the Newtonsoft.Json and JavaScriptSerializer libraries, except instead of using [JsonIgnore] you would use [ScriptIgnore].
Be aware that the JavaScriptSerializer is in the System.Web.Extensions assembly, but in the System.Web.Script.Serialization namespace. Here is the code from the Newtonsoft.Json code snippet that needs to be replaced in order to use the JavaScriptSerializer:
// In WriteFromJsonFile<T>() function replace:
var contentsToWriteToFile = Newtonsoft.Json.JsonConvert.SerializeObject(objectToWrite);
// with:
var contentsToWriteToFile = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(objectToWrite);
// In ReadFromJsonFile<T>() function replace:
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(fileContents);
// with:
return new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<T>(fileContents);
Happy Coding!
Comments
Paul Morris
Hi Daniel, I’ve been searching for a way to save a List of a class of mine, and eventually found your response to Pindo on StackOverflow, and I’ve just quickly tried saving to an XML file. In a word….brilliant!! Great work Daniel.
Omar
Great job! Thank you for sharing your clean, concise code :)
Ryan Upton
Iv’e been messing about with various ways of storing XML data. This is be cleanest method yet! Thanks!
alete
Hi Daniel, thanks for this amazing article. Anyway, I’m still having troubles trying to read all the records written into the binary file. I’ve written a list into the file, but when I read the file, the list only populates it first [0] element. Any advice?
deadlydog
@alete Without seeing some of your code it’s hard to say. I would post your problem on StackOverflow.com along with your code. It’s likely just something small that you’re overlooking.
alete
@deadlydog you are right, I did. Thanks!!! http://stackoverflow.com/questions/31257654/populating-a-dictionary-of-objects-from-objects-written-separately-in-a-file-c
jOSH
Thanks for that Dan.
Peter
Thanks Daniel,
Great code and a big help.
Craze
That is awesome!!! Thanks.
Jeff Reddy
I’m pretty sure you’ve got some code backwards in there in the json usage example. You are passing the objects to the ReadFromJsonFile as the second argument which throws a compile time error. Then when calling the WriteToJsonFile method, you’re not passing the required objects (person/people).
Here is the code you have in the example, followed by the correct code…
JsonSerialization.WriteToJsonFile(“C:\person.txt”); JsonSerialization.WriteToJsonFile<List>(“C:\people.txt”);
// Then in some other function. Person person = JsonSerialization.ReadFromJsonFile(“C:\person.txt”, person); List people = JsonSerialization.ReadFromJsonFile<List>(“C:\people.txt”, people);
Should be
JsonSerialization.WriteToJsonFile(“C:\person.txt”,person); JsonSerialization.WriteToJsonFile<List>(“C:\people.txt”, people);
// Then in some other function. Person person = JsonSerialization.ReadFromJsonFile(“C:\person.txt”); List people = JsonSerialization.ReadFromJsonFile<List>(“C:\people.txt”)
deadlydog
@Jeff Reddy Yup, you’re right. Darn copy-paste-modify bugs :P I’ve updated the post to use the correct code. Thanks!
Nathan
Worked like a charm, congratz!
chris
As someone starting out in C#, this has helped me a lot! Thanks for all the cool stuff you post. Keep it up!
Martin
Helpful, thanks. I have read, and it seems to be the case, that you cannot append to a binary file, so the append flag in the ReadFromBinaryFile is redundant.
Jim C
Works like a charm! Thx!
SHIN
You are Awesome!! Thanks.
How to save/restore serializable object to/from file? - ExceptionsHub
[…] just wrote a blog post on saving an object’s data to Binary, XML, or Json. You are correct that you must decorate your classes with the [Serializable] attribute, but only if […]
Pavel Khrapkin
I keep my XML file in VSTO Solution as Properties.Suppliers.xml and read it progammatically from Path @”....\Properties.Suppliers.xml” just with this C# code above published long time ago.
Is it any elegant way to get it “directly”, somehow similar to
var AllSuppliers =?= (new XMLserialyser(List)).Deserialyze(Properties.Supplier);
Any Json alternative for Properties file like this?
Thank you for the prompt advise
juri
Hey, thanks for this code!
How can i use it, to serialize a static class?
William
Watch it on the Newtonsoft.Json entry. It does indeed work fine, but what you get back is not your original object, it’s a Json object. Not an issue unless you start doing squirrelly stuff like reflection on what you saved/loaded. You’ll quickly find what you saved is not exactly what you get back, and for me, at least, I was very lost where all the extra stuff was coming from.
I used the example from the Stack Overflow related question, and if you .GetType() you’ll find it’s not what you saved. I am about to try the binary version and see if I get better results.
Jeremy
Thanks for this, used the binary approach, this have saved us a ton of work!!
Majid Rafei
Thank you for your great work presented here about saving data into files. When I saw your post in stackoverflow, I found out that binary method best suits my conditions. Excellent.
hiddenpackage
I enjoy coding, however, i am pretty much a novice and i dont think i am very good (yet). However, this code is so straightforward and self-explanatory, it was a joy to experiment with! Thanks for sharing!
Eduardo Glanzmann
Great solution, helped me a lot. Thank you very much.
Leee
This is what I’ve been looking for… Thank you! You’re AWESOME!
Coder1o1
Thanks for this post that describe and show how to save the objects to various formats. Performance wise which is better, if we have huge data to save or load? XML, JSON or Binary?
John Clements
Thank you! Saved me a ton of searching, and for something to simply implemented.
Leave a Comment
Your email address will not be published. Required fields are marked *