3

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?

1
  • This is the correct version of your XML: <add key="FirmwarePath" value="C:\Users\rinat\Desktop\Download.txt"/>. You could search in google for xml validation. Commented Jan 18, 2016 at 9:15

3 Answers 3

3

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();
Sign up to request clarification or add additional context in comments.

2 Comments

when i add app.config and copy the code i have a warning Message: 1 Could not find schema information for the element 'Default'. maybe i need XML file instead app.config?
just ignore the message for now and have a try.
3

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,.

1 Comment

i get null in string when using it
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.