2012-11-27 54 views
0

假設下面的XML,什麼是正確的自定義配置的代碼?:什麼是我的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

而且我覺得我需要:

  • ServiceTestElementCollectio N:ConfigurationElementCollection中
  • ServiceTestGroupsElement:配置節
+0

看看[此帖](http://www.codeproject.com/文章/ 32490/Custom-Configuration-Sections-for-Lazy-IDs)上CodeProject –

回答

1

這不是你有上面的確切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> 

下面是代碼:

using System; 
using System.Configuration; 

namespace TestConfigurationElement 
{ 
    public class ServicesMonitorSection : ConfigurationSection 
    { 
     [ConfigurationProperty("serviceTestGroups", IsRequired = true)] 
     public ServiceTestGroupElementCollection ServiceTestGroups 
     { 
      get { return (ServiceTestGroupElementCollection)this["serviceTestGroups"]; } 
      set { this["serviceTestGroups"] = value; } 
     } 
    } 

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

     [ConfigurationProperty("serviceTests", IsRequired = true)] 
     public ServiceTestElementCollection ServiceTests 
     { 
      get { return (ServiceTestElementCollection)this["serviceTests"]; } 
      set { this["serviceTests"] = value; } 
     } 
    } 

    public class ServiceTestGroupElementCollection : ConfigurationElementCollection 
    { 
     protected override ConfigurationElement CreateNewElement() 
     { 
      return new ServiceTestGroupElement(); 
     } 

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

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

     [ConfigurationProperty("expectedResponseTime", IsRequired = true)] 
     public int ExpectedResponseTime 
     { 
      get { return (int)this["expectedResponseTime"]; } 
      set { this["expectedResponseTime"] = value; } 
     } 
    } 

    public class ServiceTestElementCollection : ConfigurationElementCollection 
    { 
     protected override ConfigurationElement CreateNewElement() 
     { 
      return new ServiceTestElement(); 
     } 

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

    public static class Program 
    { 
     public static void Main(string[] args) 
     { 
      ServicesMonitorSection section = (ServicesMonitorSection)ConfigurationManager.GetSection("servicesMonitor"); 
     } 
    } 
} 
+0

太棒了!謝啦。你給了我正確的方向,我找出了其餘的。您唯一遺漏的是使用ConfigurationCollection屬性來更改「添加」動詞以匹配我的原始XML。 – theBoringCoder

+0

你能分享嗎?我很想知道改變了什麼。我一直是學生。 – taylorjonl

0

這是我提出的最終解決方案。我只需要稍微修改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> 

感謝,

湯姆

相關問題