2009-12-31 42 views
3

我有以下代碼如何解決「無法識別的元素'elementName'。(line x)(line x)」?

var section = new CustomConfigurationSection(); 
section.SectionInformation.Type = "System.Configuration.NameValueFileSectionHandler"; 
section.SectionInformation.SetRawXml(sectionXml); 
configuration.Sections.Add(sectionName, section); 

最後一行其中拋出:

ConfigurationErrorsException錯誤 發生執行用於顯示器的配置 部的處理程序。

與內部異常:

無法識別的元素的屏幕「。 (線 1)(第1行)CustomConfigurationSection的

定義:

public class CustomConfigurationSection: ConfigurationSection 
{ 
    public CustomConfigurationSection() 
    { 
    } 
} 

配置是一個自定義的類,它有一個屬性名爲部分,即具有類型 'ConfigurationSectionCollection' 的一個實例。

而且在sectionXml傳入的XML是:

<monitor> 
    <screens> 
    <screen> 
     <regions> 
     <region> 
      <labelCoordinates /> 
      <startupApplication>Internet</startupApplication> 
      <color /> 
      <width>426</width> 
      <height>266</height> 
      <x1>0</x1> 
      <x2>0</x2> 
      <y1>0</y1> 
      <y2>0</y2> 
     </region> 
     </regions> 
     <height>800</height> 
     <width>1280</width> 
    </screen> 
    <screen> 
     <regions /> 
     <height>0</height> 
     <width>0</width> 
    </screen> 
    </screens> 
</monitor> 

我怎樣才能得到這個工作?

+0

請發佈您的完整CustomConfigurationSection類和任何支持類。 – 2009-12-31 18:33:04

回答

7

查看你的例子之後,我可以看到它不起作用的一個原因是你正在使用NameValueFileSectionHandler。如果我沒有記錯,這隻允許以下語法:

<YourSectionName> 
    <add key="monitor.region.x" value="0"/> 
<YourSectionName> 

基於xml你想使用你可能需要完全實現配置節類。所以,你必須像下面這樣:

class ServiceResponseSection : ConfigurationSection 
{ 
    [ConfigurationProperty("ServiceResponses")] 
    [ConfigurationCollection(typeof(ServiceResponse), AddItemName = "addServiceResponse", RemoveItemName = "removeServiceResponse", ClearItemsName = "clearServiceResponses")] 
    public ServiceResponses ServiceResponses 
    { 
     get { return this["ServiceResponses"] as ServiceResponses; } 
    } 

} 

public class ServiceResponses : ConfigurationElementCollection 
{ 
    public override ConfigurationElementCollectionType CollectionType 
    { 
     get 
     { 
      return ConfigurationElementCollectionType.AddRemoveClearMap; 
     } 
    } 

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

    public new ServiceResponse this[string responseString] 
    { 
     get 
     { 
      return (ServiceResponse)this.BaseGet(responseString); 
     } 
     set 
     { 
      if (this.BaseGet(responseString) != null) 
      { 
       this.BaseRemoveAt(this.BaseIndexOf(this.BaseGet(responseString))); 
      } 
      this.BaseAdd(value); 
     } 
    } 

    public void Add(ServiceResponse ServiceResponse) 
    { 
     this.BaseAdd(ServiceResponse); 
    } 

    public void Clear() 
    { 
     this.BaseClear(); 
    } 

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

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((ServiceResponse)element).ResponseString; 
    } 

    public void Remove(ServiceResponse element) 
    { 
     BaseRemove(element.ResponseString); 
    } 

    public void Remove(string responseString) 
    { 
     BaseRemove(responseString); 
    } 

    public void RemoveAt(int index) 
    { 
     BaseRemoveAt(index); 
    } 

} 

public class ServiceResponse : ConfigurationElement 
{ 
    private int m_tryCount; 

    public ServiceResponse() 
    { 
     this.m_tryCount = 0; 
    } 

    [ConfigurationProperty("responseString")] 
    public string ResponseString 
    { 
     get { return (String)this["responseString"]; } 
     set { this["responseString"] = value; } 
    } 

    [ConfigurationProperty("matchWholeString")] 
    public bool MatchWholeString 
    { 
     get 
     { 
      return (bool)this["matchWholeString"]; 
     } 
     set 
     { 
      this["matchWholeString"] = value.ToString(); 
     } 
    } 

    [ConfigurationProperty("retryCount")] 
    public int RetryCount 
    { 
     get 
     { 
      return (int)this["retryCount"]; 
     } 
     set 
     { 
      this["retryCount"] = value.ToString(); 
     } 
    } 

    [ConfigurationProperty("failsProcess")] 
    public bool FailsProcess 
    { 
     get 
     { 
      return (bool)this["failsProcess"]; 
     } 
     set 
     { 
      this["failsProcess"] = value.ToString(); 
     } 
    } 

    public int TryCount 
    { 
     get { return this.m_tryCount; } 
     set { this.m_tryCount = value; } 
    } 

    public void Reset() 
    { 
     this.m_tryCount = 0; 
    } 

} 

這將然後使用XML這樣的:

<ServiceResponseList> 
    <ServiceResponses> 
     <clearServiceResponses/> 
     <addServiceResponse responseString="API Server Login Error" matchWholeString="false" retryCount="5" failsProcess="false"/> 
    </ServiceResponses> 
</ServiceResponseList> 

主要的一點是,我使用的是集合(你在你的例子有,其中有幾個)。在這個集合中,我代表了一個對象。所以爲了讓它解析xml,你必須創建一個部分處理程序來匹配你正在使用的。

根據我的例子中,你可能會想你的XML沿着線更改爲:

<monitor> 
    <screens> 
    <screen height="800" width="1280"> 
     <regions> 
     <region startupApplication="Internet" width="426" height="266" x1="0" x2="0" y1="0" y2="0"/> 
    </regions> 
    </screen> 
    </screens> 
</monitor> 

然後,您可以使用類似我的例子類。雖然你可能會得到你想要的東西,但是你需要使用其他配置項來使它以這種方式工作。

希望有所幫助。