c# - app.config 中的嵌套自定义元素 (C#)

标签 c# xml nested app-config

G'da 给大家,

几个小时以来,我一直在努力弄清楚如何从 app.config 读取设置。文件:

<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="Databases" type="McFix.DatabaseSection, McFix"/>
  </configSections>

  <Databases>
    <Database name="database">
      <Tables>
        <Table name="be_sessions">
          <Columns>
            <Column name="sess_id">
            </Column>
          </Columns>
        </Table>
      </Tables>
    </Database>
  </Databases>

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

自定义处理程序类的代码是 here , 也复制如下:

public class DatabaseSection : ConfigurationSection
{
    [ConfigurationProperty("Databases", IsDefaultCollection = false)]
    public DatabaseInstanceCollection Databases
    {
        get { return (DatabaseInstanceCollection)this["Databases"]; }
        set { this[""] = value; }
    }
}
[ConfigurationCollection(typeof(DatabaseElement), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap )]
public class DatabaseInstanceCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new DatabaseElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((DatabaseElement)element).Name;
    }
}
public class DatabaseElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }
}

public class TableSection : ConfigurationSection
{
    [ConfigurationProperty("Tables", IsDefaultCollection = true)]
    public TableInstanceCollection Tables
    {
        get { return (TableInstanceCollection)this["Tables"]; }
        set { this[""] = value; }
    }
}

[ConfigurationCollection(typeof(TableElement), AddItemName = "Table", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class TableInstanceCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new TableElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((TableElement)element).Name;
    }
}

public class TableElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }
}

public class ColumnSection : ConfigurationSection
{
    [ConfigurationProperty("Columns", IsDefaultCollection = true)]
    public ColumnInstanceCollection Columns
    {
        get { return (ColumnInstanceCollection)this["Columns"]; }
        set { this[""] = value; }
    }
}

[ConfigurationCollection(typeof(ColumnElement), AddItemName = "Column", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class ColumnInstanceCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new ColumnElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ColumnElement)element).Name;
    }
}

public class ColumnElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }
}

问题是当我尝试通过 GetSection 方法获取“数据库”部分时:

Configuration Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
DatabaseSection DbConfig = Config.GetSection("Databases") as DatabaseSection;

程序抛出 ConfigurationErrorsException,报告​​“Unrecognized element 'Database'”,尽管它在通过 DatabaseSection 的 get 方法后执行了此操作,即使我将 DatabaseInstanceCollection 的 AddItemName 定义为“Database”。我是否遗漏了一些东西,可以让底层代码正确读取 app.config 的属性?

最佳答案

强制链接:

粗略看了一下,您的问题似乎出在这一行:

[ConfigurationCollection(typeof(DatabaseElement), AddItemName = "add", CollectionType = ConfigurationElementCollectionType.BasicMap )]

这表明 .config 文件应如下所示:

<Databases>
    <add><!-- Database goes here --></add>
</Databases>

即您的 Databases 元素需要一个“添加”子元素,以指示要将一个项目添加到集合中。

您应该尝试将 AddItemName 属性从“add”更改为“Database”:

[ConfigurationCollection(typeof(DatabaseElement), AddItemName = "Database", CollectionType = ConfigurationElementCollectionType.BasicMap )]

(我还没有机会测试这个,可能还有其他问题)

关于c# - app.config 中的嵌套自定义元素 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5575880/

相关文章:

c# - 控制 XML namespace 的顺序

bash,嵌套命令和重定向

arrays - 退出嵌套 For 循环 VBA 并重新循环

java - 锁和嵌套的同步方法

c# - 在 C# 中,在 2 个线程之间传递数据的推荐方式是什么?

java - 从 java 对象创建 xml 文件

c# - 如何发送带有查询字符串的 URL 作为查询字符串

python - XML 输出包括 setup_module 和 teardown_module

c# - 使用 Autofac 和 EF Core 3.1 的内存泄漏(从 2.2 迁移后)

c# - 可空性分析无法对来自 NameValueCollection 的 null 发出警告。为什么?