2015-02-05 15 views
0

我想要實現的是讀一個app.config PARAM看起來像這樣:如何將配置參數存儲爲元素的正文?

<SomeConfig> 
     <SomeParam>SomeText</SomeParam> 
    </SomeConfig> 

代碼財產申報是這樣

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

不過,我收到此錯誤信息在應用程序啓動:「Property'SomeParam'不是ConfigurationElement」
我該如何正確聲明它?

+0

[檢查] [1]這些鏈接可能會幫助你。 [1]:http://stackoverflow.com/questions/7044871/how-do-i-use-net-custom-configurationelement-properties-on-descendent-elements –

+0

@H。瑪希達,不幸的是,它不能,因爲我需要將該值存儲爲元素體而不是元素屬性。 – user626528

回答

1

解決方案:

您的App.config應該是這樣的:

的App.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="SomeConfig" type="ConfigReader.SomeConfigSection,ConfigReader" /> 
    </configSections> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <SomeConfig> 
    <SomeParam>SomeText</SomeParam> 
    </SomeConfig> 
</configuration> 

Program.cs的

using System; 
using System.Configuration; 
using System.Xml; 

namespace ConfigReader 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      SomeConfigSection configSection = ConfigurationManager.GetSection("SomeConfig") as SomeConfigSection; 

      if (configSection != null) 
       Console.WriteLine("Value={0}", configSection.SomeParam.Value);  
     } 
    } 
    public class SomeConfigSection : ConfigurationSection 
    { 
     [ConfigurationProperty("SomeParam")] 
     public SomeParamElement SomeParam 
     { 
      get { return this["SomeParam"] as SomeParamElement; } 
      set { this["SomeParam"] = value; } 
     } 
    } 

    public class SomeParamElement:ConfigurationElement 
    { 
     protected override void DeserializeElement(XmlReader reader, bool s) 
     { 
      Value = reader.ReadElementContentAs(typeof(string), null) as string; 
     } 
     public string Value { get; private set; } 
    } 
} 

編輯:截圖

enter image description here

0

我想你會需要重寫OnDeserializeUnrecognizedElement。請看看this answer

使用上面的方法,這裏是我是如何能夠達到您的要求的結果: -

我SomeConfigSection類如下所示: -

public class SomeConfigSection : ConfigurationSection 
{ 
    [ConfigurationProperty("SomeConfig", IsRequired = true)] 
    public string SomeConfig 
    { 
     get { return (string)base["SomeConfig"]; } 
     set { base["SomeConfig"] = value; } 
    } 

    XElement _SomeParam; 
    public XElement SomeParam 
    { 
     get { return _SomeParam; } 
    } 

    protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader) 
    { 
     if (elementName == "SomeParam") 
     { 
      _SomeParam = (XElement)XElement.ReadFrom(reader); 
      return true; 
     } 
     else 
      return base.OnDeserializeUnrecognizedElement(elementName, reader); 
    } 
} 

我的App.config看起來是這樣的: -

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="SomeConfig" type="ConfigTest.SomeConfigSection,ConfigTest" /> 
    </configSections> 
    <SomeConfig> 
    <SomeParam>SomeText</SomeParam> 
    </SomeConfig> 
</configuration> 

在我的形式,下面是我如何讀值: -

SomeConfigSection configSection = ConfigurationManager.GetSection("SomeConfig") as SomeConfigSection; 

    if (configSection != null) 
     label1.Text= configSection.SomeParam.Value; 

希望這會有所幫助!

+0

它在這種情況下似乎不起作用,因爲在處理程序被我的屬性元素調用之前拋出異常。 – user626528

+0

你好,謝謝你的回覆。我用更適合我的代碼更新了回覆。如果異常來自其他地方,那麼我們需要分析堆棧跟蹤。對於我來說,上面的代碼似乎正確地給出了結果。 – amitthk