Recommend classes for handling xml configuration file in C #?

I am writing a program that should store some configuration information. I thought XML would be a good choice, but I don't want to learn XML in C # from scratch.

Could you recommend some good ways / classes handling this task?

+6
c # xml configuration
source share
4 answers

Just use the integrated .NET configuration system! What is wrong with this?

To do this, add a reference to System.Configuration in your project and study the ConfigurationManager class in this namespace well.

Starting with Visual Studio 2005, you can also define application or user parameters in the visual designer inside VS - see Overview of application parameters in MSDN for this (they work for Winforms and console applications). These settings are stored in your app.config / web.config for the application area settings and in a separate user.config file in the directory accessible to the user account for user range settings. They can be managed and updated at run time using standard .NET classes (from the System.Configuration namespace).

+7
source share

Although I agree that β€œreinventing the wheel” is not a good idea, the .NET System.Configuration class is pretty hard to use for such a simple task.

I needed to create separate configuration files that could be specified on the command line for one of my programs (instead of just using the default, it would be "app.config"), so I used this simple class that saves itself into an xml file.

http://www.dot-dash-dot.com/files/UIsettings.vb

+1
source share

Here is a simple little snippet to get you started. You do not need to know any xml ...

  internal static string GetConfigSetting(string settingName) { return ConfigurationManager.AppSettings[settingName].ToString(); } internal static bool GetConfigSettingBool(string settingName) { string setting = GetConfigSetting(settingName); if (string.IsNullOrEmpty(setting)) return false; bool value = false; if (bool.TryParse(setting, out value)) return value; else return false; } internal static string[] GetConfigSettingsArray(string settingName) { return GetConfigSetting(settingName).Split(','); } internal static DateTime? GetConfigSettingDateTime(string settingName) { string setting = GetConfigSetting(settingName); DateTime retval; if (DateTime.TryParse(setting, out retval)) { return retval; } else { return null; } } internal static void SetConfigSetting(string settingName, string value){ ConfigurationManager.AppSettings[settingName] = value; } 
0
source share

I have been using an XML configuration type setting for some time, which starts from the inline configuration, which is pretty neat. Basically, you can give it a hint and it will deserialize the xml directly into an object for you. I think the original source from which I got this was here , but I am not 100% sure, so if someone knows the author, edit his answer to give me the proper credit.

Here is a class that actually does deserialization:

 using System.Configuration; using System.Xml; using System.Xml.Serialization; using System.Xml.XPath; namespace Ariel.Configuration { class XmlSerializerSectionHandler : IConfigurationSectionHandler { public object Create(object parent, object configContext, XmlNode section) { XPathNavigator nav = section.CreateNavigator(); string typename = (string)nav.Evaluate("string(@type)"); Type t = Type.GetType(typename); XmlSerializer ser = new XmlSerializer(t); return ser.Deserialize(new XmlNodeReader(section)); } } } 

Now you can create any class you want to customize.

 namespace MyProject.Config { public class TestConfig { /// <summary> /// Whether the session is enabled /// </summary> public bool Enabled { get; set; } } } 

Now you need the app.config file. I usually prefer the style of using a separate configuration file for each configuration class, so I will give you this as an example.

 <?xml version="1.0"?> <configuration> <configSections> <section name="TestConfig" type="Ariel.Configuration.XmlSerializerSectionHandler, Ariel"/> </configSections> <runtime> <gcServer enabled="true"/> </runtime> <TestConfig configSource="Config\\TestConfig.config" /> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> 

The important parts are: section name = "TestConfig" <- Configuration class type = Ariel.Configuration.XmlSerializerSectionHandler, Ariel <- The type that is used for deserialization is the xml in the object. This basically tells the configuration manager what to run to create our TestConfig object configSource = "Config \ TestConfig.config" <- This tells us where to find the configuration file for the TestConfig section.

In the TestConfig.config file, you have something like this:

 <?xml version="1.0" encoding="utf-8" ?> <TestConfig type="MyProject.Config.TestConfig, MyProject"> <Enabled>True</Enabled> </TestConfig> 

Again, the important part is the reference to the class name and type.

Last but not least, load the configuration:

 using System.Configuration; Config.SessionKillConfig config = null; try { config = (Config.SessionKillConfig)ConfigurationManager.GetSection("CdrAnalyzerConfig"); } catch (System.Configuration.ConfigurationException ex) { syslog.Fatal("Loading parser configuration failed", ex); return; } if(config.Enabled) { //do stuff } 

It may be a little long, but the fact is that it is quite portable. Basically, to enable configuration, all you have to do is write a class and then write an xml file corresponding to this.

I am not 100% sure how to pass the configuration file as a parameter to the program, but I believe that in this case there is an option in the ConfigurationManager class. I even think I read somewhere that someone is working to detect changes in the file and reload the new configuration into the running program, but I have not tried anything like this. In any case, I hope this example is useful for one approach that you can take.

0
source share

All Articles