我有一个web.config文件,我想检索特定键名的连接字符串值。
<connectionStrings>
<add name="abc" connectionString="Server=(local);Initial Catalog=abc;Integrated Security=SSPI;Max Pool Size=25" providerName="System.Data.SqlClient" />
<add name="cde" connectionString="Server=(local);Initial Catalog=cde; Integrated Security=SSPI;Max Pool Size=50" providerName="System.Data.SqlClient" />
</connectionStrings>
我知道我可以通过configurationManager获取连接字符串,但我想通过XML阅读器获取它。目前我正在使用
XDocument document = XDocument.Load(fullPath);
var connectionString = from c in document.Descendants(connectionStrings)
select c ;
我正在获取连接字符串。但我想获得特定的“abc”连接字符串。能帮帮我吗
答案 0 :(得分:1)
XDocument document = XDocument.Load(fullPath);
var connectionString = from c in document.Descendants("connectionStrings").Descendants("add")
where c.Attribute("name").Value == "abc"
select c;
答案 1 :(得分:0)
试试这个
XDocument document = XDocument.Load(fullPath);
var connectionString = from c in document.Descendants("connectionStrings")
where c.name=="abc"
select c ;
答案 2 :(得分:0)
另一种选择(使用流利的语法)
var connectionString = document.Descendants("connectionStrings")
.Descendants("add")
.First(x => x.Attribute("name").Value == "abc").Attribute("connectionString").Value;