A day with .Net

My day to day experince in .net

Storing connection string in XML and read it

Posted by vivekcek on July 23, 2009

When i was developing an inventory for my shop i done this code,about 1 year ago,Now i am just documenting it from old source files avilable in my recycle bin.

The XML file I used to hold SQL server info was like this

<?xml version="1.0" encoding="us-ascii" ?> 
- <!-- Making any cahnges to this file cause application error
  --> 
- <ConnectionString>
  <server>(local)</server> 
  <uid>sa</uid> 
  <pwd /> 
  <database>eStock</database> 
  </ConnectionString>

And the c# code to read this given below

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace eStock
{
    class xmlop
    {
        public string cons=string.Empty,server,uid,pwd,database;
        public string GetConString()
        {
            try
            {
                XmlTextReader xmlreadr = new XmlTextReader("config.xml");
                xmlreadr.WhitespaceHandling = WhitespaceHandling.None;
                xmlreadr.MoveToContent();
                XmlReader readr = xmlreadr;
                server = readr.ReadElementString();
                uid = readr.ReadElementString();
                pwd = readr.ReadElementString();
                database = readr.ReadElementString();
                readr.Close();
                cons = "server=" + server + ";uid=" + uid + ";pwd=" + pwd + ";database=" + database;
                return cons;


            }
            catch (Exception ex)
            {
                 return(ex.Message);
            }
            
        
        }

    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s