简体   繁体   English

如何从属性webconfig c#中获取值?

[英]How can I get a value from attribute webconfig c#?

I need to get a single value from attribute element, my webconfig is like this: 我需要从属性元素中获取单个值,我的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>

How can I get " userReport " as value,this value belongs to the subsection "ReportingService", I need the value just for assign to a label. 如何将“ userReport ”作为值,此值属于“ReportingService”小节,我需要将值仅用于分配给标签。 I get a section like this: 我得到这样一个部分:

public class SeccionConfig : ConfigurationSection
    {

       [ConfigurationProperty("ReportingService")]

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

        }
    }

My ConfigurationCollection class like this: 我的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);
            }
        }
     }

And my ConfigurationElement like this: 我的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;
            }
        }
    }
}

How can I do? 我能怎么做?

尝试这个 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM