【问题标题】:C# How to create custom section without parent nodeC#如何创建没有父节点的自定义部分
【发布时间】:2024-05-03 22:55:02
【问题描述】:

我已通过以下文章工作。

http://www.jokecamp.com/blog/net-custom-configuration-section-collection-and-elements/

如果我确实喜欢它所说的,我可以实现。但我想删除父节点。我希望 xml 如下所示。

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="jobSection"
      type="MyConfiguration.JobSection, MyConfiguration" />
  </configSections>
  <jobSection>
      <job id="1" name="Job Name A" />
      <job id="2" name="Job Name B" />
  </jobSection>
</configuration>

如果 xml 像上面那样,我会收到错误 Unrecognized element 'job'.。 如何通过上面的 xml 定义自定义部分?

【问题讨论】:

    标签: c# web-config app-config configurationmanager custom-sections


    【解决方案1】:

    我认为您的帖子与以下帖子相似。请查看可能对您有帮助的帖子。

    Unrecognized element "Item" in config file with custom config section

    【讨论】:

      【解决方案2】:

      在您的配置类中,您应该在集合上定义 ConfigurationProperty 属性,名称为空字符串,IsDefaultCollection 属性设置为 true:

      [ConfigurationProperty("", IsDefaultCollection = true)]  
      public JobsCollection Jobs  
      

      【讨论】: