Extract List or Array data from .NET Framework AppConfig or .NET Core AppSetting file

When a programmer writes code, he or she doesn’t want to have every specific custom data and settings in the code. Instead, they want to have those written in a configuration file. This article will introduce how to set an object list in .Net Framework App.config and .Net Core Appsetting.json respectively and also how to extract those data from a configuration file.

.Net Core

appsettings.json

The diagram above shows how to write EmailList object list inside appsetting.json with ToEmails array. In order to extra a list object data from appsetting.json, you need to download these packages.

Create a class EmailSetting which matches to the Email array data structure.

The main program(Program.cs)

Firstly, set the JSON file’s name and its path

Then we need to decide which section of the JSON file you want to extract. This article will parse the Object EmailList with its ToEmails array.

To print all the data from the array EmailSetting., the print output result is shown below.

.NET Framework

App.config

After setting the email array inside App.config, you need to parse the XML structure of App.config. emailSettings has two properties address and displayName, we need to be able to parse these two properties’ value. The diagram provided below shows how to parse XML structure with emailList list object

The above figure will crawl the tree structure of the XML file. When the XML node attribute name is the same as the emailSetting attribute name, it will be added to the myConfigObject(EmailSetting) collection.

EmailSetting object

Program.cs

Parse App.config and prints out Email array values

It is better to set important settings and data in a configuration file as this helps developers to customize the settings based on different environments. Knowing how to write and extra complex data structures in App.config(.Net Framework) or appsettings.json (Net Core), this will give .Net developers one more choice when dealing with localization data and settings