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.
microsoft.com/en-us/library/182eeyhh.aspx

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
            Console.WriteLine(config.buffsize);

            // change data
            config.buffsize = 1024;

            // save config
            Config.SaveConfigData(config);

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

            Console.ReadKey(true);
        }
    }

    public class Config
    {
        #region Default Data
        private const int DEF_BUFF_SIZE = 2048;
        private const string DEF_LOCAL_GATE_IP = "192.168.2.2";
        private const string DEF_TARGET_IP = "192.168.2.10";
        private static readonly int[] DEF_PORTS = new int[] {
            3500,
            4500,
        };
        #endregion

        // 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="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<buffsize>1024</buffsize>
<local_gate_ip>192.168.2.2</local_gate_ip>
<target_ip>192.168.2.10</target_ip>
<ports>
<
int>3500</int>
<int>4500</int>
</ports>
</
ConfigData>

Up Next
    Ebook Download
    View all
    Learn
    View all