Multiple int values in config key C# XML

809 views Asked by At

I want to add multiple int values to my Settings. So far I have this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1" 
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"   
requirePermission="false"/>
</configSections>

<DigiPortServer1>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</DigiPortServer1>
</configuration>

Is this right? I have found many questions considerung multiple String values. How can I access these values? I would like to save these into a int array or something similar.

2

There are 2 answers

9
NicoRiff On BEST ANSWER

I would modify your file to be in this way. So it will be easier to access and to work with. This way you can easily access those features by writing ConfigurationManager.AppSettings["3"].ToString(); Note that your file wasn´t having the keys associated with any values. I added the value atribute.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"    
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers" 
requirePermission="false"/>
</configSections>

<appSettings>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</appSettings>
</configuration>
0
Balaji Marimuthu On

You could access the configuration section value using the ConfigurationManager GetSection method.

var section = System.Configuration.ConfigurationManager.GetSection("DigiPortServer1") as System.Collections.Specialized.NameValueCollection;
        var value = section["keyname"];

If section is the name value pair then cast with above type (NameValueCollection) or you can use own type to cast.