How to search custom config section by key
我有自定义配置部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="DBConfiguration" type="NewSQLExecuter.DBConfigurationSection, NewSQLExecuter"/> </configSections> <DBConfiguration> <Items> </Items> </DBConfiguration> </configuration> |
我已经编写了一个从自定义配置部分读取数据的类…类代码如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace NewSQLExecuter { public class DBConfigurationSection : ConfigurationSection { [ConfigurationProperty("Items")] public ItemsCollection Items { get { return ((ItemsCollection)(base["Items"])); } } } [ConfigurationCollection(typeof(ItemsElement))] public class ItemsCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new ItemsElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((ItemsElement)(element)).CountryCode; } public ItemsElement this[int idx] { get { return (ItemsElement)BaseGet(idx); } } } public class ItemsElement : ConfigurationElement { [ConfigurationProperty("servername", DefaultValue ="", IsKey = false, IsRequired = false)] public string ServerName { get { return ((string)(base["servername"])); } set { base["servername"] = value; } } [ConfigurationProperty("dbname", DefaultValue ="", IsKey = false, IsRequired = false)] public string DBName { get { return ((string)(base["dbname"])); } set { base["dbname"] = value; } } [ConfigurationProperty("userid", DefaultValue ="", IsKey = false, IsRequired = false)] public string UserID { get { return ((string)(base["userid"])); } set { base["userid"] = value; } } [ConfigurationProperty("password", DefaultValue ="", IsKey = false, IsRequired = false)] public string Password { get { return ((string)(base["password"])); } set { base["password"] = value; } } [ConfigurationProperty("countrycode", DefaultValue ="", IsKey = true, IsRequired = true)] public string CountryCode { get { return ((string)(base["countrycode"])); } set { base["countrycode"] = value; } } } } |
这样,我迭代自定义配置数据并获取值
2代码可以正常工作,但我想再添加一个功能,如通过任何键值搜索任何配置数据,如
1 2 3 4 | if(section.key.userid=="joy") { string pwd=section.key.password } |
因此,请指导我如何将搜索功能添加到我的类中。如果我可以使用LINQ来搜索任何自定义配置数据,那就太好了。所以请引导我谢谢。
处理app.config文件中的两个自定义配置节时出错我的app.config文件看起来像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="DBConfiguration" type="CSRAssistant.DBConfigurationSection, CSRAssistant"/> <section name="LoginConfiguration" type="CSRAssistant.LoginConfigurationSection, CSRAssistant"/> </configSections> <DBConfiguration> </DBConfiguration> <LoginConfiguration> </LoginConfiguration> </appSettings> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0"/> <requiredRuntime version="v4.0.20506"/> </startup> </configuration> |
要处理两个部分,我的代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | namespace CSRAssistant { public class DBConfigurationSection : ConfigurationSection { //[ConfigurationProperty("Items")] //public ItemsCollection Items //{ // get { return ((ItemsCollection)(base["Items"])); } //} [ConfigurationProperty("", IsDefaultCollection = true)] public ItemsCollection Items { get { return ((ItemsCollection)(base[""])); } } } [ConfigurationCollection(typeof(ItemsElement))] public class ItemsCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new ItemsElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((ItemsElement)(element)).CountryCode; } public ItemsElement this[int idx] { get { return (ItemsElement)BaseGet(idx); } } } public class ItemsElement : ConfigurationElement { [ConfigurationProperty("servername", DefaultValue ="", IsKey = false, IsRequired = false)] public string ServerName { get { return ((string)(base["servername"])); } set { base["servername"] = value; } } [ConfigurationProperty("dbname", DefaultValue ="", IsKey = false, IsRequired = false)] public string DBName { get { return ((string)(base["dbname"])); } set { base["dbname"] = value; } } [ConfigurationProperty("userid", DefaultValue ="", IsKey = false, IsRequired = false)] public string UserID { get { return ((string)(base["userid"])); } set { base["userid"] = value; } } [ConfigurationProperty("password", DefaultValue ="", IsKey = false, IsRequired = false)] public string Password { get { return ((string)(base["password"])); } set { base["password"] = value; } } [ConfigurationProperty("countrycode", DefaultValue ="", IsKey = true, IsRequired = true)] public string CountryCode { get { return ((string)(base["countrycode"])); } set { base["countrycode"] = value; } } } public class LoginConfigurationSection : ConfigurationSection { //[ConfigurationProperty("Items")] //public ItemsCollection Items //{ // get { return ((ItemsCollection)(base["Items"])); } //} [ConfigurationProperty("", IsDefaultCollection = true)] public ItemsCollection Items { get { return ((ItemsCollection)(base[""])); } } } public class LoginElement : ConfigurationElement { [ConfigurationProperty("UserName", DefaultValue ="", IsKey = false, IsRequired = false)] public string UserName { get { return ((string)(base["UserName"])); } set { base["UserName"] = value; } } [ConfigurationProperty("Pwd", DefaultValue ="", IsKey = false, IsRequired = false)] public string Pwd { get { return ((string)(base["Pwd"])); } set { base["Pwd"] = value; } } [ConfigurationProperty("Country", DefaultValue ="", IsKey = false, IsRequired = false)] public string Country { get { return ((string)(base["Country"])); } set { base["Country"] = value; } } } } |
这样读取两个自定义配置设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | string joyPassword =""; LoginConfigurationSection LoginConfigurationSection = (LoginConfigurationSection)ConfigurationManager.GetSection("LoginConfiguration"); if (LoginConfigurationSection != null) { var UserCredentials = LoginConfigurationSection.Items .Cast<LoginElement>() .FirstOrDefault(_element => _element.UserName =="razi"); if (UserCredentials != null) joyPassword = UserCredentials.Country; DBConfigurationSection section = (DBConfigurationSection)ConfigurationManager.GetSection("DBConfiguration"); if (section != null) { var DbConnection = section.Items .Cast<ItemsElement>() .FirstOrDefault(_element => _element.CountryCode.ToUpper() == joyPassword.ToUpper()); if (DbConnection != null) joyPassword = DbConnection.ServerName; } } |
现在我像在运行时一样出错了无法识别的属性"username"。请注意,属性名区分大小写。
为什么无法识别的属性'username'只是不理解……如果可能,请指导我。谢谢
你可以这样做:
1 2 3 4 5 6 | var joyUserElement = section.Items .Cast<ItemsElement>() .FirstOrDefault(_element => _element.UserID =="joy"); if (joyUserElement != null) string joyPassword = joyUserElement.Password; |
这对你有意义吗?我可能在这里遗漏了一些东西,因为这没有经过测试,但它应该可以正常工作。
我看到你用的是普通的
1 | foreach(ItemElement element in section.Items) |
我还想补充两点:
我建议您重命名元素类,以反映它的实际情况,例如,
DbConfigurationElement 而不是ItemElement 。从长远来看,这将使代码更加清晰。如果
标记没有特殊含义(看起来是这样的),那么可以在XML中"隐藏"它。如果像这样注释集合属性
.
2然后您可以这样编写分区:
1 2 3 4 5 | <DBConfiguration> </DBConfiguration> |
这对于像您这样的小型、单个集合配置部分非常有用。
嗨,这样就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="DBConfiguration" type="CSRAssistant.DBConfigurationSection, CSRAssistant"/> <section name="LoginConfiguration" type="CSRAssistant.LoginConfigurationSection, CSRAssistant"/> </configSections> <DBConfiguration> </DBConfiguration> <LoginConfiguration> </LoginConfiguration> </appSettings> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0"/> <requiredRuntime version="v4.0.20506"/> </startup> </configuration> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace CSRAssistant { public class DBConfigurationSection : ConfigurationSection { //[ConfigurationProperty("Items")] //public ItemsCollection Items //{ // get { return ((ItemsCollection)(base["Items"])); } //} [ConfigurationProperty("", IsDefaultCollection = true)] public ItemsCollection Items { get { return ((ItemsCollection)(base[""])); } } } [ConfigurationCollection(typeof(ItemsElement))] public class ItemsCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new ItemsElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((ItemsElement)(element)).CountryCode; } public ItemsElement this[int idx] { get { return (ItemsElement)BaseGet(idx); } } } public class ItemsElement : ConfigurationElement { [ConfigurationProperty("servername", DefaultValue ="", IsKey = false, IsRequired = false)] public string ServerName { get { return ((string)(base["servername"])); } set { base["servername"] = value; } } [ConfigurationProperty("dbname", DefaultValue ="", IsKey = false, IsRequired = false)] public string DBName { get { return ((string)(base["dbname"])); } set { base["dbname"] = value; } } [ConfigurationProperty("userid", DefaultValue ="", IsKey = false, IsRequired = false)] public string UserID { get { return ((string)(base["userid"])); } set { base["userid"] = value; } } [ConfigurationProperty("password", DefaultValue ="", IsKey = false, IsRequired = false)] public string Password { get { return ((string)(base["password"])); } set { base["password"] = value; } } [ConfigurationProperty("countrycode", DefaultValue ="", IsKey = true, IsRequired = true)] public string CountryCode { get { return ((string)(base["countrycode"])); } set { base["countrycode"] = value; } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace CSRAssistant { //****login config public class LoginConfigurationSection : ConfigurationSection { //[ConfigurationProperty("Items")] //public ItemsCollection Items //{ // get { return ((ItemsCollection)(base["Items"])); } //} [ConfigurationProperty("", IsDefaultCollection = true)] public LoginCollection Items { get { return ((LoginCollection)(base[""])); } } } [ConfigurationCollection(typeof(LoginElement))] public class LoginCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new LoginElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((LoginElement)(element)).ID; } public LoginElement this[int idx] { get { return (LoginElement)BaseGet(idx); } } } public class LoginElement : ConfigurationElement { [ConfigurationProperty("UserName", DefaultValue ="", IsKey = false, IsRequired = false)] public string UserName { get { return ((string)(base["UserName"])); } set { base["UserName"] = value; } } [ConfigurationProperty("Pwd", DefaultValue ="", IsKey = false, IsRequired = false)] public string Pwd { get { return ((string)(base["Pwd"])); } set { base["Pwd"] = value; } } [ConfigurationProperty("Country", DefaultValue ="", IsKey = false, IsRequired = false)] public string Country { get { return ((string)(base["Country"])); } set { base["Country"] = value; } } [ConfigurationProperty("ID", DefaultValue ="", IsKey = false, IsRequired = false)] public string ID { get { return ((string)(base["ID"])); } set { base["ID"] = value; } } } } |
呼叫方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | private void button1_Click_1(object sender, EventArgs e) { string _country =""; string _ServerName =""; LoginConfigurationSection LoginConfigurationSection = (LoginConfigurationSection)ConfigurationManager.GetSection("LoginConfiguration"); if (LoginConfigurationSection != null) { var UserCredentials = LoginConfigurationSection.Items .Cast<LoginElement>() .FirstOrDefault(_element => _element.UserName =="razi"); if (UserCredentials != null) _country = UserCredentials.Country; DBConfigurationSection section = (DBConfigurationSection)ConfigurationManager.GetSection("DBConfiguration"); if (section != null) { var DbConnection = section.Items .Cast<ItemsElement>() .FirstOrDefault(_element => _element.CountryCode.ToUpper() == _country.ToUpper()); if (DbConnection != null) _ServerName = DbConnection.ServerName; } } } |