C# 如何按键搜索自定义配置节
我有像这样的自定义配置部分C# 如何按键搜索自定义配置节,c#,linq,custom-configuration,C#,Linq,Custom Configuration,我有像这样的自定义配置部分 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="DBConfiguration" type="NewSQLExecuter.DBConfigurationSection, NewSQLExecuter"/> </configSections>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DBConfiguration" type="NewSQLExecuter.DBConfigurationSection, NewSQLExecuter"/>
</configSections>
<DBConfiguration>
<Items>
<add servername="192.168.50.2\db1" dbname="test1" userid="sa" password="2222m@n" countrycode="GB" />
<add servername="192.168.60.2\db2" dbname="test2" userid="sa" password="22222n" countrycode="US" />
<add servername="192.168.70.2\db3" dbname="test3" userid="sa" password="3333" countrycode="DE" />
</Items>
</DBConfiguration>
</configuration>
通过这种方式,我迭代自定义配置数据并获得值
DBConfigurationSection section = (DBConfigurationSection)ConfigurationManager.GetSection("DBConfiguration");
if (section != null)
{
DateTime satrt = DateTime.Now;
for (int i = 0; i <= section.Items.Count - 1; i++)
{
var country = section.Items[i].CountryCode; ;
var constring = string.Format("{0}{1}{2}{3}", "UID=" + section.Items[i].UserID, ";PWD=" + section.Items[i].Password,
";Server=" + section.Items[i].ServerName, ";Database=" + section.Items[i].DBName);
dicList.Add(country, constring);
}
}
因此,请指导我如何将搜索功能添加到我的类中。如果我可以使用LINQ搜索任何自定义配置数据,那就太好了。所以请引导我,谢谢
处理app.config文件中的两个自定义配置节时出错
我的app.config文件看起来像
<?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>
<add servername="dbname" dbname="BBAJobBoardForGB" userid="sa" password="222" countrycode="GBR" />
<add servername="db2" dbname="BBAJobBoardForUS" userid="sa" password="swww" countrycode="USA" />
</DBConfiguration>
<LoginConfiguration>
<add UserName="ww" Pwd="ww" Country="GBR"/>
<add UserName="ss" Pwd="ss" Country="USA"/>
<add UserName="dd" Pwd="dd" Country="CAD"/>
</LoginConfiguration>
<appSettings>
<add key="MailID" value="tridip@bba-reman.com" />
</appSettings>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
<requiredRuntime version="v4.0.20506"/>
</startup>
</configuration>
以这种方式读取两个自定义配置设置
string-joyPassword=“”;
LoginConfigurationSection LoginConfigurationSection=(LoginConfigurationSection)ConfigurationManager.GetSection(“LoginConfiguration”);
如果(LoginConfigurationSection!=null)
{
var UserCredentials=loginconconfigurationsection.Items
.Cast()
.FirstOrDefault(\u element=>\u element.UserName==“razi”);
if(UserCredentials!=null)
joyPassword=UserCredentials.Country;
DBConfigurationSection=(DBConfigurationSection)ConfigurationManager.GetSection(“DBConfiguration”);
if(节!=null)
{
var DbConnection=section.Items
.Cast()
.FirstOrDefault(\u元素=>\u元素.CountryCode.ToUpper()==joyPassword.ToUpper());
if(DbConnection!=null)
joyPassword=DbConnection.ServerName;
}
}
现在我得到了运行时的错误
无法识别的属性“UserName”。请注意,属性名称区分大小写。
为什么无法识别的属性“UserName”不明白…如果可能,请指导我。谢谢您可以这样做:
var joyUserElement = section.Items
.Cast<ItemsElement>()
.FirstOrDefault(_element => _element.UserID == "joy");
if (joyUserElement != null)
string joyPassword = joyUserElement.Password;
foreach(ItemElement element in section.Items)
<DBConfiguration>
<add servername="192.168.50.2\db1" dbname="test1" userid="sa" password="2222m@n" countrycode="GB" />
<add servername="192.168.60.2\db2" dbname="test2" userid="sa" password="22222n" countrycode="US" />
<add servername="192.168.70.2\db3" dbname="test3" userid="sa" password="3333" countrycode="DE" />
</DBConfiguration>
我想补充两点:
- 我建议您重命名元素类以反映它的实际情况,例如
而不是DbConfigurationElement
。从长远来看,这将使代码更加清晰itemeelement
- 如果xml中的
标记没有特殊意义(似乎是这样),则可以将其“隐藏”在xml中。如果像这样注释集合属性
var joyUserElement = section.Items
.Cast<ItemsElement>()
.FirstOrDefault(_element => _element.UserID == "joy");
if (joyUserElement != null)
string joyPassword = joyUserElement.Password;
foreach(ItemElement element in section.Items)
<DBConfiguration>
<add servername="192.168.50.2\db1" dbname="test1" userid="sa" password="2222m@n" countrycode="GB" />
<add servername="192.168.60.2\db2" dbname="test2" userid="sa" password="22222n" countrycode="US" />
<add servername="192.168.70.2\db3" dbname="test3" userid="sa" password="3333" countrycode="DE" />
</DBConfiguration>
这对于像您这样的小型单集合配置部分非常有用。您好,您可以这样做
<?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>
<add servername="192.168.88.2\bbareman" dbname="BBAJobBoardForGB" userid="sa" password="8B@R5m@n" countrycode="GBR" />
<add servername="192.168.1.2\bbareman" dbname="BBAJobBoardForUS" userid="sa" password="8B@R5m@n" countrycode="USA" />
<add servername="192.168.2.2\bbareman" dbname="BBAJobBoardForDE" userid="sa" password="8B@R5m@n" countrycode="DEU" />
<add servername="192.168.77.10\bbareman" dbname="BBAJobBoardForFR" userid="sa" password="8B@R5m@n" countrycode="FRA" />
<add servername="192.168.3.2\bbareman" dbname="BBAJobBoardForIT" userid="sa" password="8B@R5m@n" countrycode="ITA" />
<add servername="192.168.4.2\bbareman" dbname="BBAJobBoardForCA" userid="sa" password="8B@R5m@n" countrycode="CAD" />
<add servername="192.168.55.10\bbareman" dbname="BBAJobBoardForES" userid="sa" password="8B@R5m@n" countrycode="ESP" />
</DBConfiguration>
<LoginConfiguration>
<add ID="1" UserName="suanguite" Pwd="gguite" Country="GBR"/>
<add ID="2" UserName="sujoy" Pwd="sujoyUS" Country="USA"/>
<add ID="3" UserName="sujoy" Pwd="sujoyCA" Country="CAD"/>
<add ID="4" UserName="pjasu" Pwd="pjasuDE" Country="DEU"/>
<add ID="5" UserName="kankana" Pwd="kankana123" Country="ITA"/>
<add ID="6" UserName="test" Pwd="test" Country="IND"/>
<add ID="7" UserName="biswajit" Pwd="biswajitES" Country="ESP"/>
<add ID="8" UserName="suparna" Pwd="suparna" Country="CAD"/>
<add ID="9" UserName="razi" Pwd="raziFR" Country="FRA"/>
<add ID="10" UserName="tridip" Pwd="tridip" Country="GBR"/>
<add ID="11" UserName="tridip" Pwd="tridip" Country="ESP"/>
<add ID="12" UserName="tridip" Pwd="tridip" Country="FRA"/>
</LoginConfiguration>
<appSettings>
<add key="MailID" value="tridip@bba-reman.com" />
</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;
}
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统配置;
命名空间CSrasistant
{
公共类DBConfigurationSection:ConfigurationSection
{
//[配置属性(“项目”)]
//公共项目收集项目
//{
//获取{return((ItemsCollection)(base[“Items”]);}
//}
[ConfigurationProperty(“,IsDefaultCollection=true)]
公共项目收集项目
{
得到
{
返回((ItemsCollection)(基本[“”]);
}
}
}
[ConfigurationCollection(typeof(ItemsElement))]
公共类项集合:ConfigurationElementCollection
{
受保护的覆盖ConfigurationElement CreateNewElement()
{
返回新的ItemsElement();
}
受保护的覆盖对象GetElementKey(ConfigurationElement元素)
{
返回((ItemsElement)(element)).CountryCode;
}
公共项选择此[int idx]
{
得到
{
返回(ItemsElement)BaseGet(idx);
}
}
}
公共类ItemsElement:ConfigurationElement
{
[ConfigurationProperty(“servername”,DefaultValue=“”,IsKey=false,IsRequired=false)]
公共字符串服务器名
{
得到
{
返回((字符串)(基[“服务器名]));
}
设置
{
base[“servername”]=值;
}
}
[ConfigurationProperty(“dbname”,DefaultValue=“”,IsKey=false,IsRequired=false)]
公共字符串DBName
{
得到
{
return((string)(base[“dbname”]);
}
设置
{
base[“dbname”]=值;
}
}
[ConfigurationProperty(“userid”,DefaultValue=“”,IsKey=false,IsRequired=false)]
公共字符串用户ID
{
得到
{
返回((字符串)(基[“userid]”));
}
设置
{
base[“userid”]=值;
}
}
[ConfigurationProperty(“密码”,DefaultValue=“”,IsKey=false,IsRequired=false)]
公共字符串密码
{
得到
{
返回((字符串)(基[“密码]));
}
设置
{
基本[“密码”]=值;
}
}
[ConfigurationProperty(“countrycode”,DefaultValue=“”,IsKey=true,IsRequired=true)]
公共字符串国家代码
{
得到
{
返回((字符串)(基[“国家代码]));
}
设置
{
基数[“countrycode”]=值;
}
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统配置;
命名空间CSrasistant
{
//****登录配置
公共类登录配置部分:Co
<DBConfiguration>
<add servername="192.168.50.2\db1" dbname="test1" userid="sa" password="2222m@n" countrycode="GB" />
<add servername="192.168.60.2\db2" dbname="test2" userid="sa" password="22222n" countrycode="US" />
<add servername="192.168.70.2\db3" dbname="test3" userid="sa" password="3333" countrycode="DE" />
</DBConfiguration>
<?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>
<add servername="192.168.88.2\bbareman" dbname="BBAJobBoardForGB" userid="sa" password="8B@R5m@n" countrycode="GBR" />
<add servername="192.168.1.2\bbareman" dbname="BBAJobBoardForUS" userid="sa" password="8B@R5m@n" countrycode="USA" />
<add servername="192.168.2.2\bbareman" dbname="BBAJobBoardForDE" userid="sa" password="8B@R5m@n" countrycode="DEU" />
<add servername="192.168.77.10\bbareman" dbname="BBAJobBoardForFR" userid="sa" password="8B@R5m@n" countrycode="FRA" />
<add servername="192.168.3.2\bbareman" dbname="BBAJobBoardForIT" userid="sa" password="8B@R5m@n" countrycode="ITA" />
<add servername="192.168.4.2\bbareman" dbname="BBAJobBoardForCA" userid="sa" password="8B@R5m@n" countrycode="CAD" />
<add servername="192.168.55.10\bbareman" dbname="BBAJobBoardForES" userid="sa" password="8B@R5m@n" countrycode="ESP" />
</DBConfiguration>
<LoginConfiguration>
<add ID="1" UserName="suanguite" Pwd="gguite" Country="GBR"/>
<add ID="2" UserName="sujoy" Pwd="sujoyUS" Country="USA"/>
<add ID="3" UserName="sujoy" Pwd="sujoyCA" Country="CAD"/>
<add ID="4" UserName="pjasu" Pwd="pjasuDE" Country="DEU"/>
<add ID="5" UserName="kankana" Pwd="kankana123" Country="ITA"/>
<add ID="6" UserName="test" Pwd="test" Country="IND"/>
<add ID="7" UserName="biswajit" Pwd="biswajitES" Country="ESP"/>
<add ID="8" UserName="suparna" Pwd="suparna" Country="CAD"/>
<add ID="9" UserName="razi" Pwd="raziFR" Country="FRA"/>
<add ID="10" UserName="tridip" Pwd="tridip" Country="GBR"/>
<add ID="11" UserName="tridip" Pwd="tridip" Country="ESP"/>
<add ID="12" UserName="tridip" Pwd="tridip" Country="FRA"/>
</LoginConfiguration>
<appSettings>
<add key="MailID" value="tridip@bba-reman.com" />
</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;
}
}
}
}
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;
}
}
}