简体   繁体   English

如何从Web.config XML文件获取属性值

[英]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.

相关问题 如何从web.config文件中获取连接字符串? - How to fetch connection string from web.config file? 如何从 web.config 文件中的 xml 参数中检索值? - How to retrieve value from xml parameter in web.config file? 从web.config中的appsettings中读取文件属性 - Read file attribute from appsettings from web.config 如何在 Web.config 中设置 Xml 文件的路径并将 Xml 中的数据绑定到下拉列表中 - How to set the path of Xml file in Web.config and bind the data from Xml into drop down 如何将存储在 web.config 中的值添加到 c# 中的类属性(来自 web 配置文件而不是资源文件) - How can I add a value that is stored in web.config to a class attribute in c#(from web config file not the resource file) 如何以编程方式从web.config的属性“maxAllowedContentLength”获取值? - How to get the value from attribute “maxAllowedContentLength” of web.config programmatically? 从ASP.NET中的Web.config文件读取值 - Reading Values from Web.config file in ASP.NET 在Web.config文件中使用数据库中的列表字符串或值 - Use a list string or values from database in Web.config file 正确的值不是来自web.config文件 - Proper values are not coming from web.config file 如何将值从web.config应用程序密钥获取到列表 - How to get values from web.config app key to a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM