Link to home
Start Free TrialLog in
Avatar of yossikally
yossikally

asked on

How to add hierarchical data to app.config? How to retrieve it later?

In my Visual C# app I have an app config file as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
      <add key="sizex" value="111" />
      <add key="sizey" value="211" />
  </appSettings>
</configuration>

which works fine, but I want to to look as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <size>
      <add key="x" value="111" />
      <add key="y" value="211" />
    </size>
  </appSettings>
</configuration>

and now I get an exception when trying to access the information
(Unrecognized element 'size'.)

can someone please show how to write it in the app.config file and how to retrieve it IN CODE?
Avatar of Bhavesh Shah
Bhavesh Shah
Flag of India image


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
    <add key="Setting1" value="Very" />
    <add key="Setting2" value="Easy" />
 </appSettings>
</configuration>

--------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace AppSettings
{
   class Program
   {
     static void ShowConfig()
     {

        // For read access you do not need to call OpenExeConfiguraton

        foreach(string key in ConfigurationManager.AppSettings)
        {
           string value = ConfigurationManager.AppSettings[key];
           Console.WriteLine("Key: {0}, Value: {1}", key, value);
        }
     }

     static void Main(string[] args)
     {

        ShowConfig();

        // Open App.Config of executable

        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration
                    (ConfigurationUserLevel.None);

        // Add an Application Setting.

        config.AppSettings.Settings.Add("ModificationDate",
                       DateTime.Now.ToLongTimeString() + " ");

        // Save the changes in App.config file.

        config.Save(ConfigurationSaveMode.Modified);

        // Force a reload of a changed section.

        ConfigurationManager.RefreshSection("appSettings");
        ShowConfig();
      }
   }
}

-------------------------------------------------
Expected Output:

Key: Settings1, Value: Very
Key: Setting2, Value: Easy
Key: Settings1, Value: Very
Key: Setting2, Value: Easy
Key: Modification Date, Value: 01:21:03
Avatar of yossikally
yossikally

ASKER

Sorry but you did not answer my question..
I don't want my app.config to be
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
    <add key="Setting1" value="Very" />
    <add key="Setting2" value="Easy" />
 </appSettings>
</configuration>

but

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appSettings>
  <ChildNode>
    <add key="Setting1" value="Very" />
    <add key="Setting2" value="Easy" />
  <ChildNode/>
 </appSettings>
</configuration>


i.e. hierarchical like I wrote

okk...

actually dont have depth knowldege

but this way can be done??

might u Already did it
static void ShowConfig()
     {

        // For read access you do not need to call OpenExeConfiguraton

        foreach(string key in ConfigurationManager.AppSettings.childNode)
        {
           string value = ConfigurationManager.AppSettings.childNode[key];
           Console.WriteLine("Key: {0}, Value: {1}", key, value);
        }
     }

Open in new window

No doesn't work..

Raising ponts to 500
You cannot do it in app.config. It has a fixed schema which is dumped into the AppSettings variable.
Why not save your information in a separate xml?
Just an XML or use any of the predefined settings files?
If my own XML, what dot net components help me with that? (Parsing, retrieving, etc.)
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dddd