0

我想在我的 web.config 中使用嵌套列表。我看到一些有效的代码,但不是我的,当我尝试阅读配置部分时出现错误:

System.Configuration.ConfigurationErrorsException : Élément non reconnu IsochroneServicesTestImbrique'。

我的配置:

<IsochroneServicesTest>
    <add name="TC">
      <IsochroneServicesTestImbrique>
        <add name="DEFAULT" path="success"/>
      </IsochroneServicesTestImbrique>
    </add>
  </IsochroneServicesTest>

和代码:

    [ConfigurationProperty("IsochroneServicesTest")]
    [ConfigurationCollection(typeof(CollectionIsochrone), AddItemName = "add")]
    public CollectionIsochroneType IsochroneServicesTest
    {
        get { return (CollectionIsochroneType)this["IsochroneServicesTest"]; }
        set { this["IsochroneServicesTest"] = value; }
    }

    public class CollectionIsochroneType : ConfigurationElementCollection
    {
        public void Add(ProviderElement element)
        {
            base.BaseAdd(element);
        }

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

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((IsochroneTypeElement)element).Name;
        }

        public IsochroneTypeElement this[int index]
        {
            get { return (IsochroneTypeElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        public new IsochroneTypeElement this[string Name]
        {
            get { return (IsochroneTypeElement)BaseGet(Name); }
        }

        public int IndexOf(IsochroneTypeElement element)
        {
            return BaseIndexOf(element);
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }
    }

    public class IsochroneTypeElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("IsochroneServicesTestImbrique", IsRequired = true)]
        [ConfigurationCollection(typeof(CollectionIsochrone), AddItemName = "add")]
        public CollectionIsochrone IsochroneServicesImbrique
        {
            get { return (CollectionIsochrone)this["IsochroneServicesTestImbrique"]; }
            set { this["IsochroneServicesTestImbrique"] = value; }
        }
    }

public class CollectionIsochrone : ConfigurationElementCollection
{
    public void Add(ProviderElement element)
    {
        base.BaseAdd(element);
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((IsochroneElement)element).Name;
    }

    public IsochroneElement this[int index]
    {
        get { return (IsochroneElement)BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }
            BaseAdd(index, value);
        }
    }

    public new IsochroneElement this[string Name]
    {
        get { return (IsochroneElement)BaseGet(Name); }
    }

    public int IndexOf(IsochroneElement element)
    {
        return BaseIndexOf(element);
    }

    protected override void BaseAdd(ConfigurationElement element)
    {
        BaseAdd(element, false);
    }
}

public class IsochroneElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("path", IsRequired = true)]
    public string Path
    {
        get { return (string)this["path"]; }
        set { this["path"] = value; }
    }
}

谢谢你的帮助

4

1 回答 1

0

您收到该错误是因为您缺少部分中的IsochroneServicesTestImbrique声明<configSections>。此外,至少一个类必须继承自ConfigurationSection. 请参阅此处的示例。

于 2013-01-08T01:57:16.117 回答