如何从属性web配置c#中获取值?

时间:2015-02-20 05:44:18

标签: c# asp.net configurationmanager

我需要从属性元素中获取单个值,我的webconfig是这样的:

 <configSections>
    <section name="Seccion" type="ManejoConfiguracion.SeccionConfig,ManejoConfiguracion"/>
  </configSections>
  <Seccion>
    <BD>
      <add key="name" value="dbKey"/>
      <add key="user" value="userBD"/>
      <add key="pass" value="123BD"/>
    </BD>
    <ReportingService>
    <add key="name" value="Reporting" />
    <add key="user" value="userReport" />
    </ReportingService>
  </Seccion>

我如何获得&#34; userReport &#34;作为值,此值属于子部分&#34; ReportingService&#34;,我需要该值仅用于分配标签。我得到这样一个部分:

public class SeccionConfig : ConfigurationSection
    {

       [ConfigurationProperty("ReportingService")]

        public ElementoColeccionConfig Seccion 
        {
            get { return ((ElementoColeccionConfig)(base["ReportingService"])); }

        }
    }

我的ConfigurationCollection类如下:

[ConfigurationCollection(typeof(ElementoConfig ))]
    public class ElementoColeccionConfig : ConfigurationElementCollection {
  protected override ConfigurationElement CreateNewElement()
        {
            return new ElementoConfig ();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ElementoConfig)(element)).key ;
        }

        public ElementoConfig this[int idx]
        {
            get
            {
                return (ElementoConfig)BaseGet(idx);
            }
        }
     }

我的ConfigurationElement就像这样:

public class ElementoConfig : ConfigurationElement
    {
        [ConfigurationProperty("key", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string key
        {
            get
            {
                return ((string)(base["key"]));
            }
            set
            {
                base["key"] = value;
            }
        }
        [ConfigurationProperty("value", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string value
        {
            get
            {
                return ((string)(base["value"]));
            }
            set
            {
                base["value"] = value;
            }
        }
    }
}

我该怎么办?

1 个答案:

答案 0 :(得分:0)

试试这个 -

SeccionConfig sc = ConfigurationManager.GetSection("Seccion") as SeccionConfig;