[英]How to fetch attribute values from Web.config XML file
I have a web.config file and i want to retrieve the connection string value of particular key name. 我有一个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>
I know i can fetch the connection string by configurationManager but i want to get that through XML reader. 我知道我可以通过configurationManager获取连接字符串,但是我想通过XML阅读器获取它。 Presently i am using
目前我正在使用
XDocument document = XDocument.Load(fullPath);
var connectionString = from c in document.Descendants(connectionStrings)
select c ;
I am getting both the connection string. 我正在获得两个连接字符串。 but i want to get specific "abc" connection string.
但我想获取特定的“ abc”连接字符串。 can you please help me out.
你能帮我一下吗
XDocument document = XDocument.Load(fullPath);
var connectionString = from c in document.Descendants("connectionStrings").Descendants("add")
where c.Attribute("name").Value == "abc"
select c;
Try this 尝试这个
XDocument document = XDocument.Load(fullPath);
var connectionString = from c in document.Descendants("connectionStrings")
where c.name=="abc"
select c ;
An alternative (using a little fluent syntax) 另一种选择(使用一点流利的语法)
var connectionString = document.Descendants("connectionStrings")
.Descendants("add")
.First(x => x.Attribute("name").Value == "abc").Attribute("connectionString").Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.