Solved

How to find the exact records using inner XML ?

Posted on 2014-10-10
7
133 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 35

Expert Comment

by:Miguel Oz
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

9 Experts available now in Live!

Get 1:1 Help Now