Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Low Maintenance XML Settings Mechanism

0.00/5 (No votes)
14 Mar 2015 2  
An easy way to edit and exchange XML config settings

Introduction

I needed an easy way to edit and exchange the config settings of a Windows service using a Windows Forms application.

As the .NET settings are really a hassle to accomplish something like this, I decided to write a simple XML settings class and a "SettingsManager" class that uses XML serialization to write and read the settings to a .config file.

Using the Code

Image 1

Only the Forms application (see the demo, VS2013) is included here, as discussing Windows services is out of the scope of this tip, and it is enough to demonstrate the principle.

The nice thing about the Form is that it does not need any editing when the XML Settings class has changed.
Reflection is used to extract the settings class straight into a grid:

C#
this.bindingSource1.Clear();

// Convert mySettings to grid.
foreach (var prop in SettingsManager.mySettings.GetType().GetProperties())
{
    string propValue = prop.GetValue(SettingsManager.mySettings, null).ToString();
    this.bindingSource1.Add(new ProvItem { Name = prop.Name, Value = propValue });
}

dataGridView1.DataSource = this.bindingSource1;
dataGridView1.Columns[0].ReadOnly = true;

Points of Interest

Note: If you are using an obfuscator, you will probably need to exclude the settings Form from obfuscation.

The generated settings .config file can also be viewed and edited with a normal text editor, e.g. Notepad++.

Happy serializing!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here