如何读取其他应用程序以获取connectionString .. 我有web.config文件的路径 但是它中有多个连接字符串,我想获得唯一的非注释连接字符串。
<connectionStrings>
<add name="DbConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abcd\Data\abcd.accdb;" providerName="System.Data.OleDb"/>
<!--<add name="DbConString" connectionString="Persist Security Info=False;User ID=sa;Password=123;Initial Catalog=abc;Data Source=afrt-WIN7JPN-80\SQLEXPRESS" providerName="System.Data.SqlClient"/>-->
</connectionStrings>
如何获取未评论的DbConString
答案 0 :(得分:3)
只需使用XmlDocument
string connStr = "";
XmlDocument xdoc = new XmlDocument();
xdoc.Load(WebConfigPath);
XmlNode xnodes = xdoc.SelectSingleNode("/configuration/connectionStrings");
foreach (XmlNode xnn in xnodes.ChildNodes)
{
if (xnn.NodeType == XmlNodeType.Comment)
{
}
else
{
connStr = xnn.Attributes["connectionString"].Value.ToString();
}
}
答案 1 :(得分:2)
你可以试试这个
var filePath = @"D:\PathToConfig\Web.config";
var map = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
var configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
如图所示 here!