Varshini S
asked on
How to find the exact records using inner XML ?
I have the following config file
I am using following code to find the connection string value. This method will return connection string value.
But I do not want to find all the connection string value from the above config.xml. If I send the tagName as DB2 ,findMyconnectionString function should return only the value for connection string DB2. Let me know how to achieve this using following method?
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<clear/>
<add name="DB1" connectionString="SDFJJFDSxxccfcsdfsdfdserewsdffdsfsdfsd fsfsdfdfsdfsdfpooretoert kfsfksN" providerName="System.Data.SqlClient" />
<add name="DB2" connectionString="cofsfdsfdsfsSDFJJFDSxxccfcsdfsdfdserewsdffdsfsdfsd fsfsdfdfsdfsdfpooretoert kfsfksN" providerName="System.Data.SqlClient" />
<add name="DB3" connectionString="bgffgftrytrytSDFJJFDSxxccfcsdfsdfdserewsdffdsfsdfsd fsfsdfdfsdfsdfpooretoert kfsfksN" providerName="System.Data.SqlClient" />
<add name="DB4" connectionString="@!@!#@#@#$#$SDFJJFDSxxccfcsdfsdfdserewsdffdsfsdfsd fsfsdfdfsdfsdfpooretoert kfsfksN" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
I am using following code to find the connection string value. This method will return connection string value.
But I do not want to find all the connection string value from the above config.xml. If I send the tagName as DB2 ,findMyconnectionString function should return only the value for connection string DB2. Let me know how to achieve this using following method?
public string findMyconnectionString(string tagName)
{
string connstr="";
XmlDataDocument doc = new XmlDataDocument();
doc.Load("config.exe.config");
XmlElement root = doc.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("connectionStrings");
foreach (XmlNode item in elemList)
{
if (item.HasChildNodes)
{
foreach (XmlNode child in item.ChildNodes)
{
if (child.Attributes.Count > 0)
{
foreach (XmlAttribute attribute in child.Attributes)
if (attribute.Name == "connectionString")
{ connstr=attribute.InnerXml ; }
}
}
}
}
return connstr;
}
ASKER
Miguel: You are correct. For this scenario I can not use configurationManager since the configuration file sitting outside my application folder. I am using this config file for multiple application and it is not in my application folder.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
OK, if that is the case, my previous code changed as follows:
Configuration config = ConfigurationManager.OpenExeConfiguration("Your Config Path");
var connstr = config .ConnectionStrings.ConnectionStrings["DB2 "].ConnectionString;
No need for custom code, .NET classes/methods can provide this info nicely.
ASKER
Miguel: You can use this code only if your Configuration file is in the application directory or its sub directory.
My application is in : C:\myProgram\company\crm
My configuration file is in c:\Mysettings\Application\ Settings\c onn.exe.co nfig
I tried this above code already and its not working.
My application is in : C:\myProgram\company\crm
My configuration file is in c:\Mysettings\Application\
I tried this above code already and its not working.
Oops I posted the wrong code, for your case we should use OpenMappedExeConfiguration as follows:
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = @"C:\temp\WcfTicketingService\WcfTicketingService\app.config";
Configuration config1 = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var connstr = config1.ConnectionStrings.ConnectionStrings["DB2"].ConnectionString;
I leave this code for others just in case they have a similar issue and need a compact/off the shelf code.
Open in new window
You just need one line of code to access it.