Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a repost from StackOverflow.
Here is the problem. I have a .config file in XML:
XML
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <section name="serial1">
        <key name="PortName">value COM1</key>
        <key name="Parity=">value Odd</key>
        <key name="DataBits">value 8</key>
        <key name="StopBits">value One</key>
        <key name="BaudRate">value 9600</key>
    </section>
    <section name="serial2">
        <key name="PortName">value COM2</key>
        <key name="BaudRate">value 4800</key>
        <key name="DataBits">value 8</key>
        <key name="Parity=">value Odd</key>
        <key name="StopBits">value One</key>
    </section>
</configuration>

I use the following C# code to access it:
C#
using Microsoft.Extensions.Configuration;
using System.IO.Ports;

IConfigurationRoot configRoot = new ConfigurationBuilder()
    .AddXmlFile("App.config", optional: false, reloadOnChange: false)
    .Build()
    ;
Console.WriteLine($"{configRoot.GetDebugView()}");

SerialPortSettings? portSettings = 
 configRoot.GetRequiredSection("serial1").Get<SerialPortSettings>();

public sealed class SerialPortSettings
{
    public string? PortName { get; set; }
    public Parity Parity { get; set; }
    public uint BaudRate { get; set; }
    public uint DataBits { get; set; }
    public StopBits StopBits { get; set; }
}

GetDebugView() returns a reasonable output:
C#
section:
  serial1:
    key:
      BaudRate=value 9600 (XmlConfigurationProvider for 'App.config' (Required))
        name=BaudRate (XmlConfigurationProvider for 'App.config' (Required))
      DataBits=value 8 (XmlConfigurationProvider for 'App.config' (Required))
        name=DataBits (XmlConfigurationProvider for 'App.config' (Required))
      Parity==value Odd (XmlConfigurationProvider for 'App.config' (Required))
        name=Parity= (XmlConfigurationProvider for 'App.config' (Required))
      PortName=value COM1 (XmlConfigurationProvider for 'App.config' (Required))
        name=PortName (XmlConfigurationProvider for 'App.config' (Required))
      StopBits=value One (XmlConfigurationProvider for 'App.config' (Required))
        name=StopBits (XmlConfigurationProvider for 'App.config' (Required))
    name=serial1 (XmlConfigurationProvider for 'App.config' (Required))
  serial2:
    key:
      BaudRate=value 4800 (XmlConfigurationProvider for 'App.config' (Required))
        name=BaudRate (XmlConfigurationProvider for 'App.config' (Required))
      DataBits=value 8 (XmlConfigurationProvider for 'App.config' (Required))
        name=DataBits (XmlConfigurationProvider for 'App.config' (Required))
      Parity==value Odd (XmlConfigurationProvider for 'App.config' (Required))
        name=Parity= (XmlConfigurationProvider for 'App.config' (Required))
      PortName=value COM2 (XmlConfigurationProvider for 'App.config' (Required))
        name=PortName (XmlConfigurationProvider for 'App.config' (Required))
      StopBits=value One (XmlConfigurationProvider for 'App.config' (Required))
        name=StopBits (XmlConfigurationProvider for 'App.config' (Required))
    name=serial2 (XmlConfigurationProvider for 'App.config' (Required)) 

But GetRequiredSection() throws an exception:
System.InvalidOperationException: 'Section 'serial1' not found in configuration.'

What am I doing wrong? Are there any special considerations on section names?

What I have tried:

Tried everything I could think of...
Posted
Updated 19-Oct-23 5:06am
v2

1 solution

Have you tried to get the section with XDocument? You can try the below code to get the desired section:
C#
// Replace config_file_path with the actual path to your config file.
XDocument doccs = XDocument.Load("config_file_path"); 
XElement sectionsd = doccs.Root.Elements("section").Where(a => a.Attribute("name").Value == "serial1").FirstOrDefault();

You can parse the XElement into your target object through XmlSerializer. However, there are some issues in the config XML structure that need to be correct as well, fixed the typo in the "Parity" attribute in the section.
 
Share this answer
 
Comments
Alex Markov 2023 19-Oct-23 8:40am    
Hi Imran,
Thank you for reply!
Sure, I can parse XML in multiple ways, just want to use the elegant way provided (supposed to) by Microsoft.Extensions.Configuration.
But life does not seem to be so easy... :-(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900