How can I read another application to get connectionString .. I have path of web.config File But there are multiple connection strings in it and I want to get the only non commented connection string.
<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>
How can I get the non commented DbConString
Done by simply reading Web.Config with 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();
}
}
You can try this
var filePath = @"D:\PathToConfig\Web.config";
var map = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
var configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
As shown here!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With