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

How do I store a config param as element's body?

我想要实现的是读取一个 app.config 参数,如下所示:

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

代码属性声明是这样的

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

但是,我在应用程序启动时收到此错误消息:"Property 'SomeParam' is not a ConfigurationElement"
如何正确申报?

解法:

您的 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; }
    }
}

编辑: 截图

我认为您需要覆盖 OnDeserializeUnrecognizedElement . Please take a look at this answer

使用上述方法,这是我如何能够达到您要求的结果:-

我的 SomeConfigSection class 看起来像这样:-

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;

希望对您有所帮助!