Change XML configuration automatically .Net

Yurii K
2 min readAug 27, 2020

--

In real life every project has a different configuration for different environments or sometimes you need to change connection strings for databases. Doing those things manually probably is not the best idea.

How can we do it? One of the solutions is to make an application that can change those configurations how we want.

Here is an XML configuration with connection strings etc.

We need to create our own XMLChanger class. That class will have 2 methods. First will change connection strings, second will change parameters in ‘MySecton’.

7 and 8 lines have a namespace for working with XML configuration. Main method ‘Change’ load configuration in memory change after that save it.

Method ‘ChangeDBConfigs’ set a new connection string for an item with name ‘Connection1’.

Method ‘ChangeMySectionConfigs’ is more complicated because ‘MySection’ has a lot of inner items. We just need to find the right one. In our case we go like this MySection -> SubSection -> add -> MyItem.

This application can read parameters from other files and change our configuration directly in the project.

If you need to take a close look on project here is the link.

Originally published at http://tomorrowmeannever.com on August 27, 2020.

--

--

Yurii K
Yurii K

Written by Yurii K

Full-stack developer. In my work, I use Angular and C#. In my free time I write articles for my blog.

No responses yet