Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C# 4.0
Tip/Trick

A Low Maintenance XML Settings Mechanism

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
14 Mar 2015CPOL 9.3K   106   9   1
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
Software developer in the Netherlands, currently working on Video Surveillance applications.
Experience: C#, C++, VB, ASP, SQL Server, PostgreSQL, Gitea, TeamCity.
It all started with Black&White, no not the whiskey but the Sinclair ZX81 followed by several Atari's and PC's. The journey continues ...

Comments and Discussions

 
NewsSource code updated Pin
RickZeeland20-Mar-15 7:54
mveRickZeeland20-Mar-15 7:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.