我正在尝试访问配置文件中的设置,这是一系列xml元素,如下所示:
<databases> <database name="DatabaSEOne" Value="[value]" /> <database name="DatabaseTwo" Value="[value]" /> </databases>
现在我想访问它.我已经设置了这样的类:
Public Class DatabaseConfigurationHandler Inherits ConfigurationSection <ConfigurationProperty("Databases",IsDefaultCollection:=True)> _ Public ReadOnly Property Databases() As DatabaseCollection Get Return CType(Me("Databases"),DatabaseCollection) End Get End Property End Class Public Class DatabaseCollection Inherits ConfigurationElementCollection Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement Return (New Database()) End Function Protected Overloads Overrides Function GetElementKey(ByVal element As ConfigurationElement) As Object Return (CType(element,Database).DatabaseName) End Function End Class Public Class Database Inherits ConfigurationElement <ConfigurationProperty("name",IsKey:=True,Isrequired:=True)> _ Public Property DatabaseName() As String Get Return Me("name").ToString() End Get Set(ByVal Value As String) Me("name") = Value End Set End Property <ConfigurationProperty("value",Isrequired:=True)> _ Public Property DatabaseValue() As String Get Return Me("value").ToString() End Get Set(ByVal Value As String) Me("value") = Value End Set End Property End Class
Dim config As New DatabaseConfigurationHandler config = System.Configuration.ConfigurationManager.GetSection("databases/database") Return config.Databases("DatabaSEOne")
谢谢.
这是我几天前做的非常相似的剪切和粘贴.
原文链接:https://www.f2er.com/vb/255350.html配置:
<ListConfigurations> <lists> <add Name="blah" EndpointConfigurationName="blah" ListName="blah" ConnectionString="blah" TableName="blah" FieldsCsv="blah" DbFieldsCsv="blah"/> <add Name="blah2" EndpointConfigurationName="blah" ListName="blah" ConnectionString="blah" TableName="blah" FieldsCsv="blah" DbFieldsCsv="blah"/> </lists> </ListConfigurations>
配置部分C#:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace App { /// <summary> /// Individual list configuration /// </summary> class ListConfiguration : ConfigurationElement { [ConfigurationProperty("Name",IsKey = true,Isrequired = true)] public string Name { get { return (string)this["Name"]; } } [ConfigurationProperty("EndpointConfigurationName",Isrequired = true)] public string EndpointConfigurationName { get { return (string)this["EndpointConfigurationName"]; } } [ConfigurationProperty("ListName",Isrequired = true)] public string ListName { get { return (string)this["ListName"]; } } [ConfigurationProperty("ConnectionString",Isrequired = true)] public string ConnectionString { get { return (string)this["ConnectionString"]; } } [ConfigurationProperty("TableName",Isrequired = true)] public string TableName { get { return (string)this["TableName"]; } } [ConfigurationProperty("FieldsCsv",Isrequired = true)] public string FieldsCsv { get { return (string)this["FieldsCsv"]; } } [ConfigurationProperty("DbFieldsCsv",Isrequired = true)] public string DbFieldsCsv { get { return (string)this["DbFieldsCsv"]; } } } /// <summary> /// Collection of list configs /// </summary> class ListConfigurationCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new ListConfiguration(); } protected override object GetElementKey(ConfigurationElement element) { return ((ListConfiguration)element).Name; } } /// <summary> /// Config section /// </summary> class ListConfigurationSection : ConfigurationSection { [ConfigurationProperty("lists")] public ListConfigurationCollection Lists { get { return (ListConfigurationCollection)this["lists"]; } } } }
ListConfigurationSection configSection = null; try { configSection = ConfigurationManager.GetSection("ListConfigurations") as ListConfigurationSection; } catch (System.Configuration.ConfigurationErrorsException) { }