[英]How can I get a value from attribute webconfig c#?
我需要從屬性元素中獲取單個值,我的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>
如何將“ userReport ”作為值,此值屬於“ReportingService”小節,我需要將值僅用於分配給標簽。 我得到這樣一個部分:
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;
}
}
}
}
我能怎么做?
嘗試這個 -
SeccionConfig sc = ConfigurationManager.GetSection("Seccion") as SeccionConfig;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.