C# 我的XML的正确自定义配置代码是什么?

C# 我的XML的正确自定义配置代码是什么?,c#,custom-configuration,C#,Custom Configuration,假设使用以下XML,正确的自定义配置代码是什么 <ServicesMonitor> <serviceTestGroups> <serviceTestGroup name="foo"> <serviceTest uri="http://server1/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />

假设使用以下XML,正确的自定义配置代码是什么

<ServicesMonitor>
  <serviceTestGroups>
    <serviceTestGroup name="foo">
      <serviceTest uri="http://server1/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
      <serviceTest uri="http://server2/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
      <serviceTest uri="http://server3/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
    </serviceTestGroup>
  </serviceTestGroups>
</ServicesMonitor>

我知道我需要:

  • ServiceTestElement:ConfigurationElement
我想我需要:

  • ServiceTestElementCollection:ConfigurationElementCollection
  • ServiceTestGroupsElement:ConfigurationSection

这与上面的XML不完全相同,但非常接近:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="servicesMonitor" type="TestConfigurationElement.ServicesMonitorSection, TestConfigurationElement"/>
  </configSections>
  <servicesMonitor>
    <serviceTestGroups>
      <add name="foo">
        <serviceTests>
          <add uri="http://server1/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
          <add uri="http://server2/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
          <add uri="http://server3/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
        </serviceTests>
      </add>
    </serviceTestGroups>
  </servicesMonitor>
</configuration>

这是我提出的最终解决方案。我只需要稍微修改一下taylorjonl的代码

namespace Demo.ServiceMonitor
{
    using System;
    using System.Configuration;

    public class ServiceMonitorSection : ConfigurationSection
    {
        [ConfigurationCollection(typeof(ServiceTestGroupElementCollection), AddItemName = "serviceTestGroup")]
        [ConfigurationProperty("serviceTestGroups", IsRequired = true)]
        public ServiceTestGroupElementCollection ServiceTestGroups
        {
            get { return this["serviceTestGroups"] as ServiceTestGroupElementCollection; }
            set { this["serviceTestGroups"] = value; }
        }
    }

    public class ServiceTestGroupElementCollection : ConfigurationElementCollection
    {
        public ServiceTestGroupElement this[int index]
        {
            get { return (ServiceTestGroupElement)this.BaseGet(index); }
            set
            {
                if (this.BaseGet(index) != null)
                {
                    this.BaseRemoveAt(index);
                    this.BaseAdd(index, value);
                }
            }
        }

        public ServiceTestGroupElement this[string Name]
        {
            get { return (ServiceTestGroupElement)this.BaseGet(Name); }
        }

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

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

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

        [ConfigurationCollection(typeof(ServiceTestElementCollection), AddItemName = "serviceTest")]
        [ConfigurationProperty("serviceTests", IsKey = false, IsRequired = true)]
        public ServiceTestElementCollection ServiceTests
        {
            get { return this["serviceTests"] as ServiceTestElementCollection; }
            set { this["serviceTests"] = value; }
        }
    }

    public class ServiceTestElementCollection : ConfigurationElementCollection
    {
        public ServiceTestElement this[int index]
        {
            get { return (ServiceTestElement)this.BaseGet(index); }
            set
            {
                if (this.BaseGet(index) != null)
                {
                    this.BaseRemoveAt(index);
                    this.BaseAdd(index, value);
                }
            }
        }

        public ServiceTestElement this[string Name]
        {
            get { return (ServiceTestElement)this.BaseGet(Name); }
        }

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

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ServiceTestElement)element).Uri;
        }
    }

    public class ServiceTestElement : ConfigurationElement
    {
        [ConfigurationProperty("uri", IsKey = true, IsRequired = true)]
        public Uri Uri
        {
            get { return this["uri"] as Uri; }
            set { this["uri"] = value; }
        }

        [ConfigurationProperty("expectedResponseTime", IsKey = false, IsRequired = true)]
        public int ExpectedResponseTime
        {
            get { return (int)this["expectedResponseTime"]; }
            set { this["expectedResponseTime"] = value; }
        }
    }
}
XML如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="servicesMonitor"
             type="Demo.ServiceMonitor.ServiceMonitorSection, Demo.ServiceMonitor"/>
  </configSections>
  <servicesMonitor>
    <serviceTestGroups>
      <serviceTestGroup name="Geocoding Service Tests">
        <serviceTests>
          <serviceTest uri="http://server1/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
          <serviceTest uri="http://server2/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
          <serviceTest uri="http://server3/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
        </serviceTests>
      </serviceTestGroup>
    </serviceTestGroups>
  </servicesMonitor>
</configuration>

谢谢


汤姆

看看《密码工程》真棒!谢谢,伙计。你给了我正确的方向,剩下的我都明白了。您唯一错过的是使用ConfigurationCollection属性更改“add”动词以匹配我的原始XML。您可以共享吗?我很想知道发生了什么变化。我一直是个学生。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="servicesMonitor"
             type="Demo.ServiceMonitor.ServiceMonitorSection, Demo.ServiceMonitor"/>
  </configSections>
  <servicesMonitor>
    <serviceTestGroups>
      <serviceTestGroup name="Geocoding Service Tests">
        <serviceTests>
          <serviceTest uri="http://server1/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
          <serviceTest uri="http://server2/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
          <serviceTest uri="http://server3/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
        </serviceTests>
      </serviceTestGroup>
    </serviceTestGroups>
  </servicesMonitor>
</configuration>