在每个配置文件中,Sections只能出现一次

本文关键字:一次 配置文件 Sections | 更新日期: 2023-09-27 18:10:30

我想创建一个包含许多clientUserclientUsers的"List"

 <configuration>
        <configSections>
          <sectionGroup name="clientUsers">
            <section name="clientUser" type="System.Configuration.NameValueFileSectionHandler" />
          </sectionGroup>
        </configSections>
        <clientUsers>
            <!-- user number 1  -->
            <clientUser>
              <add key="id"       value="1" />
              <add key="userName" value="someuser" />
              <add key="password" value="test" />
              <add key="IPs"      value="1,2,3" />
            </clientUser>
            <!-- user number 2  -->
            <clientUser>
              <add key="id"       value="2" />
              <add key="userName" value="avi2" />
              <add key="password" value="test" />
              <add key="IPs"      value="1,2,3" />
            </clientUser>
   </clientUsers>

为什么我得到这个错误:

Sections在每个配置文件中只能出现一次。有关异常,请参阅帮助主题。

如何创建clientUser列表

在每个配置文件中,Sections只能出现一次

我认为您正在系统下寻找ConfigurationElementCollection类。配置MSDN Link http://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.aspx

还有一个关于codeproject的教程

代码项目站点

中的一小段代码
public class ShiSettingCollection : ConfigurationElementCollection
   {
      public ShiSettingElements this[int index]
      {
         get
         {
            return base.BaseGet(index) as ShiSettingElements;
         }
         set
         {
            if (base.BaseGet(index) != null)
            {
               base.BaseRemoveAt(index);
            }
            this.BaseAdd(index, value);
         }
      }
      protected override System.Configuration.ConfigurationElement CreateNewElement()
      {
         return new ShiSettingElements();
      }
      protected override object GetElementKey(ConfigurationElement element)
      {
         return ((ShiSettingElements)element).Key;
      }
   }