rinat
rinat

Reputation: 87

Get array of parameters from xml file

i have the following app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <xx>
      <add key="x" value="1.1.1.1" />
      <add key="y" value="1.1.1.1" />
      <add key="z" value="1.1.1.1" />
      <add key="w" value="6" />
    </xx>

    <yy>
      <add key="Wireshark" value="1" /> 
    </yy>

    <zz>
      <add key="Firmware1" value="C:\Users\Desktop\Download.txt/>
      <add key="Firmware2" value="C:\Users\Desktop\Download.txt" />
    </zz>

</configuration>

how can i have an array for x, y and w. should i need appsettings ? does this xml is valid?

Upvotes: 3

Views: 627

Answers (3)

Martin
Martin

Reputation: 664

You could read custom section of config files as below,

var defaultSettings = ConfigurationManager.GetSection("Default") as NameValueCollection; //You can replace Default with any other node name like Maestro, Drive
string hostIp = defaultSettings["hostIP"].ToString(); //you can access any of the key value under Default node in your xml config now.

Note that you may have to add a reference to System.Configuration from Framework.

Upvotes: 0

daryal
daryal

Reputation: 14919

First, you need to write a custom class for each custom section in the configuration file; another option is to use one of the built-in types.

For example;

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="Default" type="System.Configuration.NameValueSectionHandler" />
        <section name="Maestro" type="System.Configuration.NameValueSectionHandler" />
        <section name="Drive" type="System.Configuration.NameValueSectionHandler" />
    </configSections>
    <Default>
      <add key="gmasIP" value="192.168.2.3" />
      <add key="hostIP" value="192.168.2.2" />
      <add key="GatewayIP" value="192.168.2.4" />
      <add key="relayCOM" value="6" />
    </Default>

    <Maestro>
      <add key="Wireshark" value="1" /> 
    </Maestro>

    <Drive>
      <add key="FirmwarePath" value="C:\Users\rinat\Desktop\Download.txt/>
      <add key="FirmwarePalPath" value="C:\Users\rinat\Desktop\Download.txt" />
    </Drive>

</configuration>

If you want to get the values as an array:

    var defaultItems= ConfigurationManager.GetSection("Default") as NameValueCollection;
    List<string> temp = new List<string>();

if (defaultItems!= null)
{
    foreach (var key in defaultItems.AllKeys)
    {
        string val= defaultItems.GetValues(key).FirstOrDefault();
        temp.Add(val);
    }
}

    string[] arr = temp.ToArray();

Upvotes: 3

Kutty Rajesh Valangai
Kutty Rajesh Valangai

Reputation: 635

This is the simple snippet taking descendants values from XML,

  string[] arr1 = XDocument.Load(@"C:\xxx.xml").Descendants("Default")
                            .Select(element => element.Value).ToArray();

 string[] arr2 = XDocument.Load(@"C:\xxx.xml").Descendants("Maestro")
                            .Select(element => element.Value).ToArray();

 string[] arr3 = XDocument.Load(@"C:\xxx.xml").Descendants("Drive")
                            .Select(element => element.Value).ToArray();

use this code,.

Upvotes: 3

Related Questions