如何将配置参数存储为元素的主体?

时间:2015-02-05 04:42:05

标签: c# .net config

我正在尝试实现的是阅读app.config参数,如下所示:

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

代码属性声明就像这样

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

但是,我在app start上收到此错误消息:“属性'SomeParam'不是ConfigurationElement”
我该如何正确宣布?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;

希望这有帮助!