Don Diego
Don Diego

Reputation: 1508

Read an XML standard config file selected from console

I created a CLI application which actually uses the standard config app.Config file.

In this file I put some subsections, like

 <typicsTable>
    <mainSettings>
      <add key="sheetNumber" value="1"/>
      <add key="firstDataRow" value="2"/>
    </mainSettings>
 </typicsTable>

I actually read these settings with

NameValueCollection TypicsConversionTableSettings = (NameValueCollection)ConfigurationManager.GetSection("typicsTable/mainSettings");

int ctSheetNumber =     Int32.Parse(TypicsConversionTableSettings["sheetNumber"]);
int ctFirstDataRow =    Int32.Parse(TypicsConversionTableSettings["firstDataRow"]);

Everything works fine in this way.

What I want to do now is

1) I want different config files with custom names (i.e. test1.config , test2.config) and take via CLI the right config file;

2) switch to a less ".net config file", and take data from a standard XML file.

I'm now focusing on the point 1, I tried different attempts, I used

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = @"C:\folderTest\conf1.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

But I absolutely don't get how to read sections AND subsections in the file. How can I do that?

Upvotes: 2

Views: 225

Answers (1)

IV.
IV.

Reputation: 9303

The class that is going to help you, I believe, is System.Xml.Linq.

using System.Xml.Linq;

So Part 1 would be load the file into an XElement:

XElement xConfig = XElement.Load("app.simulated.config");

Here's a quick demo of how you can iterate through everything and also find a single element using a matching condition.

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("Iterating the config file values and attributes...");
        Console.WriteLine("==================================================");
        XElement xConfig = XElement.Load("app.simulated.config");
        foreach (var element in xConfig.DescendantsAndSelf())
        {
            Console.WriteLine(element.Name);
            foreach (var attribute in element.Attributes())
            {
                Console.WriteLine("\t" + attribute.Name + "," + attribute.Value);
            }
        }

        Console.WriteLine();
        Console.WriteLine("Finding a value using matching conditions.");
        Console.WriteLine("==========================================");

        XElement xel =
            xConfig
            .DescendantsAndSelf()
            .FirstOrDefault(match => 
                (match.Attribute("key") != null) && 
                (match.Attribute("key").Value == "sheetNumber"));

        Console.WriteLine(
            "The value of 'sheetNumber' is " +
            xel.Attribute("value").Value
         );

        // Pause
        Console.ReadKey();
    }
}

Console Output

Clone or Download this example from GitHub.

Upvotes: 2

Related Questions