Link to home
Start Free TrialLog in
Avatar of Varshini S
Varshini S

asked on

How to find the exact records using inner XML ?

I have the following config file
<?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>

Open in new window


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;
}

Open in new window

Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Your file is a configuration file, thus you should use ConfigurationManager class:
var connstr =  System.Configuration.ConfigurationManager.ConnectionStrings["DB2 "].ConnectionString;

Open in new window

You just need one line of code to access it.
Avatar of Varshini S
Varshini S

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;

Open in new window

No need for custom code, .NET classes/methods can provide this info nicely.
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\conn.exe.config
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;

Open in new window

I leave this code for others just in case they have a similar issue and need a compact/off the shelf code.