Solved

How to find the exact records using inner XML ?

Posted on 2014-10-10
7
135 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 62

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Throws error on Decrypting String c# code 3 40
Need help on C# Linq query on object 6 51
c# string handling 1 26
Need to pass connection string through config 2 26
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!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now