C#如何创建没有父节点的自定义节

时间:2015-01-27 09:39:02

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

我在下面的文章中工作过。

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定义自定义部分?

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

我认为你的帖子与下面的帖子看起来相似。请查看帖子,它可以帮助你。

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