2

I want to create a "List" of clientUsers that contains many of 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>

Why do i get this error:

Sections must only appear once per config file. See the help topic for exceptions.

How do I create a list of clientUser

tshepang
  • 12,111
  • 21
  • 91
  • 136
SexyMF
  • 10,657
  • 33
  • 102
  • 206
  • The comments in your XML are actually written like that in your file? If they are, change // for – Icarus Oct 06 '11 at 11:20
  • You are getting the error for exactly the reason it says. **ClientUser** is only allowed to be in the file once. – Security Hound Oct 06 '11 at 11:26
  • 1
    @Ramhound - Do you have any idea how annoying is your comment ??? I am asking how to create a list of ClientUser. – SexyMF Oct 06 '11 at 11:28
  • You *did* ask why you got the error. – Lasse V. Karlsen Oct 06 '11 at 13:14
  • I think there are similar questions asked already. Check the answer to [this question](https://stackoverflow.com/questions/710449/accessing-a-custom-configuration-section-in-net). He did it with a custom configuration handler. – Andreas Oct 06 '11 at 11:38
  • Thanks but it is not the same... see my structure, it is different. thanks – SexyMF Oct 06 '11 at 11:41
  • @SexyMF: yes, that's what you need. You will not be able to have exactly that structure in your config file, but you will only need to add a node. Otherwise, remove the sectionGroup, which is not mandatory, and call your configSection `clientUsers`, and have it contain sever `clientUser` objects. – Paolo Tedesco Oct 06 '11 at 11:46
  • @SexyMF I guess you have to change your structure. I looked over his answer and I think it's everything you need to solve your issue. – Andreas Oct 06 '11 at 12:06

1 Answers1

1

I think you are looking for ConfigurationElementCollection class under System.Configuration MSDN Link http://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.aspx

There is also a tutoial on codeproject

A short snippet from the codeproject site

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;
      }
   }
Raghu
  • 2,678
  • 2
  • 31
  • 38