2

我想创建一个“列表”,clientUsers其中包含许多clientUser

 <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>

为什么我会收到此错误:

每个配置文件中的部分只能出现一次。有关例外情况,请参阅帮助主题。

如何创建一个列表clientUser

4

1 回答 1

1

我认为您正在 System.Configuration MSDN 链接http://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.aspx下寻找 ConfigurationElementCollection 类

codeproject上也有教程

来自 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;
      }
   }
于 2011-10-06T11:47:07.197 回答