Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Read Custom XML from the app.config?

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>
like image 917
Jimmy Avatar asked Jul 18 '09 21:07

Jimmy


1 Answers

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.

like image 200
Kevin Nisbet Avatar answered Oct 15 '22 06:10

Kevin Nisbet