简体   繁体   English

如何在不更改节顺序的情况下使用ConfigurationManager.GetSection(columnsSectionName)获取.config文件的内容?

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

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. 我想从中获取值并以与配置文件相同的顺序放入IDictionary中。 I`m doing this: 我正在这样做:

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

However, I m geting the <NFeColumns> in a totally different order, and I can t figure out why. 但是,我m geting the <NFeColumns> in a totally different order, and I can不知道为什么。 Can you please help me? 你能帮我么?

ConfigurationManager.GetSection returns an instance of HashTable . ConfigurationManager.GetSection返回HashTable的实例。 Items in a hash table won't be stored in the order they are inserted. 哈希表中的项目将不会按照插入顺序存储。 You will have to manually order them. 您将必须手动订购它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在类库的 App.config 中使用 ConfigurationManager.GetSection - Using ConfigurationManager.GetSection in a Class Library's App.config ConfigurationManager.GetSection()函数 - ConfigurationManager.GetSection() function ConfigurationManager.GetSection跳过重复项 - ConfigurationManager.GetSection Skips Duplicates ConfigurationManager.GetSection 返回 null - ConfigurationManager.GetSection returns null C#ConfigurationManager.GetSection无法加载文件或程序集 - C# ConfigurationManager.GetSection could not load file or assembly 如何在.Net Core 2.2中正确使用ConfigurationManager.GetSection() - How to use properly ConfigurationManager.GetSection() in .Net Core 2.2 使用ConfigurationManager的OpenExeConfiguration和GetSection,(自定义配置文件和部分) - Using ConfigurationManager's OpenExeConfiguration and GetSection,(Custom config file and section) 为什么使用ConfigurationManager.GetSection会导致“SecurityException:Request failed”,但ConfigurationManager.OpenExeConfiguration却没有? - Why does using ConfigurationManager.GetSection cause “SecurityException: Request failed” but ConfigurationManager.OpenExeConfiguration does not? ReadOnlyNameValueCollection(从ConfigurationManager.GetSection读取) - ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection) ConfigurationSection ConfigurationManager.GetSection()始终返回null - ConfigurationSection ConfigurationManager.GetSection() always returns null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM