Solved

How to read a string using InnerXml ?

Posted on 2014-10-08
9
861 Views
Last Modified: 2014-10-08
I am using following code to read a string from XML file. I have to read
a following  string from the XML file. How to read this string ?  "mcQKhSSzcMP9199vA8Bod1/y6VJrZNL3m4AQiDHwonHyuSUctBQY/cgOz5rakHa2d5mVKSSxdE2RjDDB0DxKUsoWbddaeej6ufY6fUj6ACmwJQyiB+I3OA=="



c# code:
  XmlDataDocument doc = new XmlDataDocument();
            doc.Load(@"C:\Temp\Connection.exe.config");
           
            
            XmlElement root = doc.DocumentElement;
     XmlNodeList elemList = root.GetElementsByTagName("connectionStrings");

Open in new window




XML File:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
      <clear/>
      <add name="Encrypted"
             providerName="System.Data.SqlClient"
             connectionString="mcQKhSSzcMP9199vA8Bod1/y6VJrZNL3m4AQiDHwonHyuSUctBQY/cgOz5rakHa2d5mVKSSxdE2RjDDB0DxKUsoWbddaeej6ufY6fUj6ACmwJQyiB+I3OA=="
		/>
    </connectionStrings>
    </configuration>

Open in new window

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
      <clear/>
      <add name="Encrypted"
             providerName="System.Data.SqlClient"
             connectionString="mcQKhSSzcMP9199vA8Bod1/y6VJrZNL3m4AQiDHwonHyuSUctBQY/cgOz5rakHa2d5mVKSSxdE2RjDDB0DxKUsoWbddaeej6ufY6fUj6ACmwJQyiB+I3OA=="
		/>
    </connectionStrings>
    </configuration>

Open in new window

0
Comment
Question by:Varshini S
  • 4
  • 3
  • 2
9 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40368342
Don't access your config files manually. Use the ConfigManager class instead.
0
 

Author Comment

by:Varshini S
ID: 40368372
ste5an: You are absolutely correct. But for this scenario  I need to read manually.
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40368450
The following example shows how to read the attributes of the innerXml:
using System;
using System.Xml;

namespace XmlExample
{
	class Program
	{
		static void Main(string[] args)
		{
			XmlDataDocument doc = new XmlDataDocument();
			doc.Load("Connection.exe.config");

			XmlElement root = doc.DocumentElement;
			XmlNodeList elemList = root.GetElementsByTagName("connectionStrings");
			foreach (XmlNode item in elemList)
			{
				Console.WriteLine(string.Format("Root Node - {0}; contains {1}",item.Name ,item.InnerXml));
				if (item.HasChildNodes)
				{
					foreach (XmlNode child in item.ChildNodes)
					{
						Console.WriteLine(string.Format("Child Node - {0} of Parent Node - {1}; contains {2}", child.Name, child.ParentNode.Name, child.InnerXml));
						if (child.Attributes.Count > 0)
						{
							Console.WriteLine(string.Format("Child Node - {0} contains attributes.", child.Name));
							foreach (XmlAttribute attribute in child.Attributes)
								Console.WriteLine(string.Format("Attribute {0} of Child Node - {1}; contains {2}", attribute.Name, child.Name, attribute.InnerXml));
						}
					}
				}
			}
			Console.ReadLine();
		}
	}
}

Open in new window


Produces this output:Capture.JPG
-saige-
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:ste5an
ID: 40368555
You are alreading reading your string. But I guess you want to decrypt this string.

Normally you would use an Protected Configuration Provider.

The problem is quite simple: I don't recognize this encryption schema...

So hard to tell. How was this config file generated?
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40368574
@ste5an - Offhand it looks like an encoded public or private key generated using the RSACryptoServiceProvider.

-saige-
0
 

Author Comment

by:Varshini S
ID: 40368713
ste5an - Application configuration file sitting in outside my application and more than 5 application sharing the same config file. So I have used TripleDES  encryption and generated the keys. During the run time I will decrypt the connection string.
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40368729
@Varshini - Have you looked at the code I provided to see if that leads you down the right path?  If needed I could provide more information.

-saige-
0
 

Author Comment

by:Varshini S
ID: 40368738
Saige- Thank You. This works for me
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40369247
Don't forget to accept the solution.

-saige-
0

Featured Post

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.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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