How to Create Config file with xml Serialization

This is a simple example of how to use xml serialization to make configuration files.

In many cases there is no need to use
System.Configuration namespace members for application configuration file. This article will focus on XML serialization.

The main idea is to create class which holds configuration data and then, serialize it to a file using XmlSerializer. Classes that can be serialized using XML must be declared as public and have a parameterless constructor. Also serialized members must be public.

For more information about XML serialization please refer to http://msdn.

To serialize and deserialize data use corresponding methods from XmlSerializer class (System.Xml.Serialization). Both of them operate on stream objects.

The following code sample demonstrates creating, reading and saving of simple configuration file.

using System;
using System.Xml.Serialization;
using System.IO;

namespace XMLconfigExample
    class Program
        static void Main(string[] args)
            // get configuration
            Config.ConfigData config = Config.GetConfigData();

            // use it

            // change data
            config.buffsize = 1024;

            // save config

            // test saved data
            config = Config.GetConfigData();


    public class Config
        #region Default Data
        private const int DEF_BUFF_SIZE = 2048;
        private const string DEF_LOCAL_GATE_IP = "";
        private const string DEF_TARGET_IP = "";
        private static readonly int[] DEF_PORTS = new int[] {

        // name of the .xml file
        public static string CONFIG_FNAME = "config.xml";

        public static ConfigData GetConfigData()
            if (!File.Exists(CONFIG_FNAME)) // create config file with default values
                using (FileStream fs = new FileStream(CONFIG_FNAME, FileMode.Create))
                    XmlSerializer xs = new XmlSerializer(typeof(ConfigData));
                    ConfigData sxml = new ConfigData();
                    xs.Serialize(fs, sxml);
                    return sxml;
            else // read configuration from file
                using (FileStream fs = new FileStream(CONFIG_FNAME, FileMode.Open))
                    XmlSerializer xs = new XmlSerializer(typeof(ConfigData));
                    ConfigData sc = (ConfigData)xs.Deserialize(fs);
                    return sc;

        public static bool SaveConfigData(ConfigData config)
            if (!File.Exists(CONFIG_FNAME)) return false; // don't do anything if file doesn't exist

            using (FileStream fs = new FileStream(CONFIG_FNAME, FileMode.Open))
                XmlSerializer xs = new XmlSerializer(typeof(ConfigData));
                xs.Serialize(fs, config);
                return true;

        // this class holds configuration data
        public class ConfigData
            public int buffsize;
            public string local_gate_ip;
            public string target_ip;
            public int[] ports;

            public ConfigData()
                buffsize = DEF_BUFF_SIZE;
                local_gate_ip = DEF_LOCAL_GATE_IP;
                target_ip = DEF_TARGET_IP;
ports = DEF_PORTS;

This example make/use this config.xml file:

<?xml version="1.0"?>
<ConfigData xmlns:xsi=""


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now