How do I store / retrieve the connection string in / from an XML file?
Once you get the connection string, you can store the connection string in the text file or an xml file and later retrieve it. Let’s see some example code to write a connection string to an XML file and read it backC# Version
string connStr = "";
// get connection string in the connStr variable
// Write connection string to xml file
XmlDocument xmlDoc = new XmlDocument();
XmlNode xn = xmlDoc.CreateNode(XmlNodeType.Element, "ConnectionString", "");
xn.InnerText = connStr;
xmlDoc.AppendChild(xn);
xmlDoc.Save(@"C:\ConnectionString.xml");
// ...
// Read connection string from the text file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\ConnectionString.xml");
XmlNode xn = xmlDoc.SelectSingleNode("ConnectionString");
connStr = xn.InnerText;
VB.Net Version
Dim connStr As String = ""
' get connection string in the connStr variable
' Write connection string to xml file
Dim xmlDoc As New XmlDocument
Dim xn As XmlNode
xn = xmlDoc.CreateNode(XmlNodeType.Element, "ConnectionString", "")
xn.InnerText = connStr
xmlDoc.AppendChild(xn)
xmlDoc.Save("C:\ConnectionString.xml")
' ...
' Read connection string from the xml file
Dim xmlDoc As New XmlDocument
xmlDoc.Load("C:\ConnectionString.xml")
Dim xn As XmlNode
xn = xmlDoc.SelectSingleNode("ConnectionString")
connStr = xn.InnerText
Back
Sponsored links
.Net Application Updating
One easy to use component adds safe and reliable updating features. Download today for a free trial.
One easy to use component adds safe and reliable updating features. Download today for a free trial.
Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Online Crash Analysis
Automatically capture customer crash data, no debugger required. Support for .NET, C++, OS X, Java.
Automatically capture customer crash data, no debugger required. Support for .NET, C++, OS X, Java.
.Net Localization in three simple steps (WYSIWYG)
Localize .Net, C#/C/C++ & Delphi apps visually. HTML, HTML Help, XML & databases. Try Sisulizer now!
Localize .Net, C#/C/C++ & Delphi apps visually. HTML, HTML Help, XML & databases. Try Sisulizer now!
