.NET ConfigurationErrorsException:元素在本节中可能只出现一次

时间:2023-01-19 10:27:00

I am creating a class to read/write the custom configuration from the the configuration file. Here is the configuration file

我正在创建一个类来从配置文件中读取/写入自定义配置。这是配置文件

<?xml version="1.0" encoding="utf-8" ?> 
<configuration>   
<configSections>
 <section name="Country" type="CustomeConfig.CountryConfig, CustomeConfig"/>  
</configSections>   
<Country>
    <State>
      <City>
        <add name="a" age="20"></add>
        <add name="b" age="20"></add>
        <add name="c" age="20"></add>
      </City>
      <City>
        <add name="d" age="20"></add>
        <add name="e" age="20"></add>
        <add name="f" age="20"></add>
      </City>
    </State>  
 </Country>
 </configuration>

and the code to read the configuration file is below

读取配置文件的代码如下

 namespace CustomeConfig
    {
        public class CountryConfig : ConfigurationSection
        {
        [ConfigurationProperty("State")]
        public StateCollection States
        {
            get { return ((StateCollection)(base["State"])); }
        }
    }


    [ConfigurationCollection(typeof(CityCollection))]
    public class StateCollection : ConfigurationElementCollection
    {

        protected override ConfigurationElement CreateNewElement()
        {
            return new CityCollection();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((CityCollection)(element));
        }

        [ConfigurationProperty("City", IsDefaultCollection = false)]
        public CityCollection this[int idx]
        {
            get
            {
                return (CityCollection)BaseGet(idx);
            }
        }
    }

    [ConfigurationCollection(typeof(User))]
    public class CityCollection : ConfigurationElementCollection
    {

        protected override ConfigurationElement CreateNewElement()
        {
            return new User();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((User)(element)).name;
        }

        public User this[int idx]
        {
            get
            {
                return (User)BaseGet(idx);
            }
        }

        //public override ConfigurationElementCollectionType CollectionType
        //{
        //    get { return ConfigurationElementCollectionType.BasicMap; }
        //}
    }


    public class User : ConfigurationElement
    {
        [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string name
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }
        [ConfigurationProperty("age", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string servername
        {
            get { return (string)base["age"]; }
            set { base["age"] = value; }
        }
    }

}



    static void Main(string[] args)
            {
                CountryConfig config = (CountryConfig)System.Configuration.ConfigurationSettings.GetConfig("Country");


                Console.ReadLine();
            }

as soon as I run the code it's shows the error "The element <city> may only appear once in this section". Since there are two <city> section.

一旦我运行代码,它就会显示错误“元素 在本节中可能只出现一次”。因为有两个 section >。

1 个解决方案

#1


0  

If I run into problems with configuration sections I usually start with no data in my section within the xml file. Then I populate the data via code and save it. Then you can easily figure out what you have done wrong within the xml.

如果我遇到配置部分的问题,我通常从xml文件中我的部分中没有数据开始。然后我通过代码填充数据并保存它。然后您就可以很容易地找出在xml中做错了什么。

Put this in your Custom section in order to be able to save a code populated section:

把这个放在您的自定义部分,以便能够保存一个代码填充部分:

public void Save()
{
    Configuration config = 
            ConfigurationManager.OpenExeConfiguration(spath);
    CountryConfig section = (CountryConfig)config.Sections["Country"];
    section.States = this.States; //Copy the changed data
    config.Save(ConfigurationSaveMode.Full);
}

So then you can work from your C# code to work out how the configuration should be structured (helps to spot inconsistencies between the two)

因此,您可以从c#代码中找出配置的结构(有助于发现两者之间的不一致性)

#1


0  

If I run into problems with configuration sections I usually start with no data in my section within the xml file. Then I populate the data via code and save it. Then you can easily figure out what you have done wrong within the xml.

如果我遇到配置部分的问题,我通常从xml文件中我的部分中没有数据开始。然后我通过代码填充数据并保存它。然后您就可以很容易地找出在xml中做错了什么。

Put this in your Custom section in order to be able to save a code populated section:

把这个放在您的自定义部分,以便能够保存一个代码填充部分:

public void Save()
{
    Configuration config = 
            ConfigurationManager.OpenExeConfiguration(spath);
    CountryConfig section = (CountryConfig)config.Sections["Country"];
    section.States = this.States; //Copy the changed data
    config.Save(ConfigurationSaveMode.Full);
}

So then you can work from your C# code to work out how the configuration should be structured (helps to spot inconsistencies between the two)

因此,您可以从c#代码中找出配置的结构(有助于发现两者之间的不一致性)