Simple is hard. --Steve Lautenschlager

How Can I Easily Manage an XML Configuration File in .NET?

Using .NET's built-in XML serialization features


XML serialization can be very easy in .NET. It is perfect for managing configuration files or small sets of data that don't need a full database solution.

The following code sample can be copied directly into a Visual Studio console project. Create the console project first. Then delete everything in the Class1.cs file replacing it with the code below. Compile and run.

So, the gist of it is this. Create a configuration class that will hold all of your configuration information. Then use .NET's built in XML Serialization features to read and write the XML configuration file.

One thing to watch out for when managing configuration files is versions. You will eventually realize that you wanted to store more variables in your configuration file or perhaps remove some that were there before.

For example, suppose your configuration class has a variable called Var1. Later on you realize you want to add a Var2, but there are already old config files out there that don't have a Var2. .NET will do a pretty good job of reading the XML file anyway and filling the properties it can find. In our example, there will be no value for Var2 in old XML files, so the Var2 property will be whatever it was initialized to.

But .NET may not be able to handle every situation you may face. You may find you need to do some customization, in that case it will be critical that you know the version number of the configuration file so that you can ensure it is parsed correctly. Anyway, versioning is a good practice. I learned the hard way, but you can benefit from my mistakes.

Simple Configuration File Using XML Serialization
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace Cambia
   class MainClass
      static void Main(string[] args)
         // Create a new configuration object
         // and initialize some variables
         Configuration c = new Configuration();
         c.StringItem = "My Config";
         c.IntItem = 2456;

         // Serialize the configuration object to a file
         Configuration.Serialize("config.xml", c);

         // Read the configuration object from a file
         Configuration c2 = Configuration.Deserialize("config.xml");

         // Write out the variables read from the file


   #region -- Configuration Class --
   /// <summary>
   /// This Configuration class is basically just a set of 
   /// properties with a couple of static methods to manage
   /// the serialization to and deserialization from a
   /// simple XML file.
   /// </summary>
   public class Configuration
      int _Version;
      string _StringItem;
      int _IntItem;

      public Configuration()
         _Version = 1;
         _StringItem = "";
         _IntItem = -1;
      public static void Serialize(string file, Configuration c)
         System.Xml.Serialization.XmlSerializer xs 
            = new System.Xml.Serialization.XmlSerializer(c.GetType());
         StreamWriter writer = File.CreateText(file);
         xs.Serialize(writer, c);
      public static Configuration Deserialize(string file)
         System.Xml.Serialization.XmlSerializer xs 
            = new System.Xml.Serialization.XmlSerializer(
         StreamReader reader = File.OpenText(file);
         Configuration c = (Configuration)xs.Deserialize(reader);
         return c;
      public int Version
         get { return _Version; }
         set { _Version = value; }
      public string StringItem
         get { return _StringItem; }
         set { _StringItem = value; }
      public int IntItem
         get { return _IntItem; }
         set { _IntItem = value; }


Output from running the above console application
My Config
Here's what the XML configuration file looks like
   <StringItem>My Config</StringItem>

Version: 6.0.20200920.1535