Solved

How to find the exact records using inner XML ?

Posted on 2014-10-10
7
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 36

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 36

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 36

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

729 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