How to get contents of a .config file using ConfigurationManager.GetSection(columnsSectionName) without altering the sections' order?

StackOverflow https://stackoverflow.com/questions/23377090

Question

In my config section I have the following:

<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
    <section name="NFeColumns" type="System.Configuration.DictionarySectionHandler" />
    <section name="CTeColumns" type="System.Configuration.DictionarySectionHandler" />
  </configSections>

  <appSettings>
    <add key="csv.separator" value=";" />
    <add key="infNFe.columns" value="NFeColumns"/>
    <add key="infCte.columns" value="CTeColumns"/>
    <add key="infNFe.filename" value=".\Extracted\NFeCollection.csv"/>
    <add key="infCte.filename" value=".\Extracted\CTeCollection.csv"/>
  </appSettings>

  <NFeColumns>
    <add key="infNFe.Id"  value="Id" />
    <add key="ide.cUF"    value="cUF" />
    <add key="dest.CNPJ"  value="CNPJ" />
    <add key="dest.xNome" value="xNome" />
    <add key="det.nItem"  value="nItem" />
    <add key="prod.cProd" value="cProd" />
    <add key="prod.xProd" value="xProd" />
    <add key="IPI.cEnq"   value="cEnq" />
  </NFeColumns>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

</configuration>

I want to catch the values from and put in a IDictionary in the same order as the configuration file. I`m doing this:

 string columnsSectionName = ConfigurationManager.AppSettings[colFileName + ".columns"];
            IDictionary columns = (IDictionary)ConfigurationManager.GetSection(columnsSectionName);

However, Im geting the <NFeColumns> in a totally different order, and I cant figure out why. Can you please help me?

Was it helpful?

Solution

ConfigurationManager.GetSection returns an instance of HashTable. Items in a hash table won't be stored in the order they are inserted. You will have to manually order them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top