views:

1063

answers:

3

I want to read the custom XML section from the app.config of a C# windows service.

How do I go about it?

The XML is below:

<Books>
<Book name="name1" title="title1"/>
<Book name="name2" title="title2"/>
</Books>
+3  A: 

What you want to do is read up on Custom Configuration Sections.

Colin Mackay
And good luck to noobs who are brave enough to do this! They can be very confusing for a new developer.
Will
True, the .NET 1.x way where you implemented an interface and got to play with the XML was much easier to understand, IMO.
Colin Mackay
I found this article series on codeproject (http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx) to be a good explaination on the topic.
Christian.K
+2  A: 

In a project I developed I use something similar for configuration that I found. I believe the article was called the last configuration section handler I'll ever need (I can't find a working link, maybe someone can link it for me).

This method takes what you want to do one step further, and actually de-serializes the object into memory. I'm just copying code from my project, but it should be fairly simple to take a step backwards if all you want is the XML.

First, you need to define a class that handles your configuration settings.

using System;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;


namespace Ariel.config
{
    class XmlSerializerSectionHandler : IConfigurationSectionHandler
    {

     #region IConfigurationSectionHandler Members

     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));
     }

     #endregion
    }
}

Now, say you want to load a section of configuration... super easy, cast to the type of object you're expecting to XML Serialize to, and pass the section you're looking for (in this case SearchSettings.

try
{
  config = (Eagle.Search.SearchSettings)ConfigurationSettings.GetConfig("SearchSettings");
}
catch (System.Configuration.ConfigurationException ex)
{
  syslog.FatalException("Loading search configuration failed, you likely have an error", ex);
  return;
}

Now, all you need is your App.config file. I chose to split mine into separate files (1 file per section) just to make managing the config a little easier. You define a section, give it a name, and define the type (whatever you called the class listed above) and the assembly's name.

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="SearchSettings" type="Ariel.config.XmlSerializerSectionHandler, Ariel"/>
  </configSections>
  <SearchSettings configSource="Config\Search.config" />
</configuration>

Now, all that's left is the config file to be de-serialized. What's important here is that the block matches your section name, and your type is whatever object it should de-serialize to, and the Assembly name.

<?xml version="1.0" encoding="utf-8" ?>
<SearchSettings type="Eagle.Search.SearchSettings, Eagle">
  <NumThreads>4</NumThreads>
</SearchSettings>

If you just want the pure raw XML, all you should need to do is modify the Object that handles the section to return the XML or do whatever you need to do.

Kevin Nisbet
A: 

I use custom xml in my config.app. file and create a app.XSD from it. The XSD file includes the schema of the config.app file. Then XSD file can be translated to a vb class or a C# class file using 'xsd.exe'. Now all you have to do is deserialize the configfile to the class.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="CustomConfig" type="Object" />
    </configSections>

    <CustomConfig>
        <ActiveEnvironment>QAS</ActiveEnvironment>
        <Environments>
            <Environment name ="PRD" log="Y">
            </Environment>
            <Environment name ="QAS" log="N">
            </Environment>
            <Environment name ="DEV" log="Y">
            </Environment>
        </Environments>
    </CustomConfig>

</configuration>
Pieter