自定义配置处理程序的问题-元素<在本节中只能出现一次

本文关键字:一次 程序 处理 配置 问题 -元素 自定义 | 更新日期: 2023-09-27 18:11:14

所以我写了一个自定义配置处理程序,允许我在应用程序配置文件中为单个键分配多个值。我现在的方法有效,但只适用于一个元素。我得到的错误是我试图多次使用相同的元素,但是我知道使用自定义配置处理程序这应该是完全可能的。

请赐教

以下是我的一些相关代码:App - Config
<configSections>
        <sectionGroup name="propertyValuesGroup">
          <section
            name="propertyValues"
            type="FlatFileTestCaseAutomater.ClaimHeaderSection,FlatFileFactory"
            allowLocation="true"
            allowDefinition="Everywhere"
          />
        </sectionGroup>
    <!-- Other <section> and <sectionGroup> elements. -->
  </configSections>
  <!-- Configuration section settings area. -->
  <propertyValuesGroup>
    <propertyValues>
      <claimHeader name="txnNo" nullable="yes" dataType="int" maxLength="20" />
      <claimHeader name="batchNo" nullable="yes" dataType="string" maxLength="20" />
    </propertyValues>
  </propertyValuesGroup>

</configuration>   

.

.

.

。自定义配置处理程序类:

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
namespace FlatFileTestCaseAutomater
{
    class ClaimHeaderSection : ConfigurationSection
    {
        [ConfigurationProperty("claimHeader")]
        public ClaimHeaderElement ClaimHeaderProperty
        {
            get
            {
                return (ClaimHeaderElement)this["claimHeader"];
            }
            set
            { this["claimHeader"] = value; }
        }
    }


    public class ClaimHeaderElement : ConfigurationElement
    {
        [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)]
        public String Name
        {
            get
            {
                return (String)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }
        [ConfigurationProperty("nullable", DefaultValue = "yes", IsRequired = true)]
        public String Nullable
        {
            get
            {
                return (String)this["nullable"];
            }
            set
            {
                this["nullable"] = value;
            }
        }
        [ConfigurationProperty("dataType", DefaultValue = "", IsRequired = true)]
        public String DataType
        {
            get
            {
                return (String)this["dataType"];
            }
            set
            {
                this["dataType"] = value;
            }
        }
        [ConfigurationProperty("maxLength", DefaultValue = "", IsRequired = true)]
        public string MaxLength
        {
            get
            { return (string)this["maxLength"]; }
            set
            { this["maxLength"] = value; }
        }
    }
}

。...

和调用值:

latFileTestCaseAutomater.ClaimHeaderSection config =
    (FlatFileTestCaseAutomater.ClaimHeaderSection)System.Configuration.ConfigurationManager.GetSection(
    "propertyValuesGroup/propertyValues");

自定义配置处理程序的问题-元素<在本节中只能出现一次

这里是使用iconfigationsectionhandler的解决方案。

请注意,我做得很快,没有适当的错误处理或null检查。

我用了你的app.config

自定义处理程序代码

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Xml;
namespace FlatFileTestCaseAutomater
{
    class ClaimHeaderSection : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            List<ClaimHeader> list = new List<ClaimHeader>();
            XmlNodeList claimNodeList = section.SelectNodes("claimHeader");
            foreach (XmlNode claimNode in claimNodeList)
            {
                string name = claimNode.Attributes["name"].Value;
                string nullable = claimNode.Attributes["nullable"].Value;
                string dataType = claimNode.Attributes["dataType"].Value;
                string maxLength = claimNode.Attributes["maxLength"].Value;
                list.Add(new ClaimHeader() {Name = name, DataType = dataType, MaxLength = maxLength, Nullable = nullable});
            }
            return list;
        }
    }

    public class ClaimHeader
    {
        public String Name { get; set; }
        public String Nullable { get; set; }
        public String DataType { get; set; }
        public String MaxLength { get; set; }
    }
}

呼叫码

        List<FlatFileTestCaseAutomater.ClaimHeader> config =
             (List<FlatFileTestCaseAutomater.ClaimHeader>)System.Configuration.ConfigurationManager.GetSection(
              "propertyValuesGroup/propertyValues");