Solved

How to find the exact records using inner XML ?

Posted on 2014-10-10
7
139 Views
Last Modified: 2014-10-13
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

0
Comment
Question by:Varshini S
  • 3
  • 2
7 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40374424
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.
0
 

Author Comment

by:Varshini S
ID: 40375015
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.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40375695
Hi Varshin;

Try it like this.

public string findMyconnectionString(string connectionAttributeName)
{
    string connstr = "";
    XmlDataDocument doc = new XmlDataDocument();
    doc.Load("config.exe.config");

    XmlElement root = doc.DocumentElement;
    XmlNode connectionStringNodes = root.SelectSingleNode("connectionStrings");

    foreach (XmlNode item in connectionStringNodes.ChildNodes)
    {
        if (item.Name != "add") continue;

        if (item.Name == "add")
        {
            var attributes = item.Attributes;
            if (attributes["name"].Value == connectionAttributeName)
                return attributes["connectionString"].Value;
        }
    }
    return "No Connection String found for " + tagName;
}

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40376076
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.
0
 

Author Comment

by:Varshini S
ID: 40376253
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.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40378598
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question