How to Read XML Document in C#

No.of Views978
Bookmarked0 times
Downloads 
Votes0
By  Ravish Kumar Sindhwani   On  11 Jun 2010 21:06:21
Tag : XML , How to
Many times we have a scenario like we need to use some hardcode values like our SQL connection string or some List name or any configuration settings like network credentials.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Many times we have a scenario like we need to use some hardcode values like our SQL connection string or some List name or any configuration settings like network credentials.
All these information is sensitive and there are chances that after sometimes we need to change these network credentials, or network path due to some unavoidable reasons. So if we have all these things in our .cs file than for changing this information we need to rebuild our dll.

To avoid these kinds of situations we are using a XML configuration file. In this article I am going to show how to read a xml file.

Step 1

Make one XML file and name it XML_Configuration_File.xml and place it in C:\Test\ location. The configuration file should look in the format as shown in below.

<Configuration><FName>Jack</FName><SName>Sparrow</SName></Configuration>

 
Configuration is the Parent node and FName, SName are the Child nodes.

Step 2

Open visual studio and make a console application project.

Step 3

For making any operation on XML file we need to add reference of System.XML

Step 4

Code for reading XML file

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

namespace ReadFromXML
{class Program
    {private const string sXML_File_Path = @"C:\Test\XML_Configuration_File.xml";static void Main(string[] args)
        {string sFirstName = GetXmlConfigValue("FName");string sSecondName = GetXmlConfigValue("SName");
            Console.WriteLine(sFirstName + " " + sSecondName);
            Console.Read();
        }public static string GetXmlConfigValue(string sNodeName)
        {string sNodeValue = "";
            XmlDocument xml_Document = null;
            XmlNodeList xnlNodelist = null;
            XmlNode xnParent = null;
            XmlNode xnChild = null;

        xml_Document = new System.Xml.XmlDocument();try{//Load the XML documentxml_Document.Load(sXML_File_Path);//Find the node by Tagname which should be exactle same as in our XML Configuration file.xnlNodelist = xml_Document.GetElementsByTagName(sNodeName);//Point to parent node from the Node List(In our example Configuration is the Parent Node.xnParent = xnlNodelist.Item(0);//Point to Child node.xnChild = xnParent.ChildNodes[0];//Read the value from the child node.sNodeValue = xnChild.InnerText.ToString();
        }catch (Exception ex)
        {throw ex;
        }return sNodeValue;      
        }
    
    }
}

 When we execute the program we will get the output in the below format 

Image Loading

In this way we can read a xml file.

Sample Project Source

Download source files -22 kb

 
Sign Up to vote for this article
 
About Author
 
Ravish Kumar Sindhwani
Occupation-Software Engineer
Company-
Member Type-Fresh
Location-India
Joined date-11 Jun 2010
Home Page-
Blog Page-
I born and brought up in Haryana and working as a software developer in Trivandrum. I am passionate for learning new Microsoft technologies and I believe if you are learning any new thing, just write it some where. You will never forget.
 
 
Other popularSectionarticles
    This writing does not include a full discussion or even the full details of RSS or XML. Rather, it includes a nice introduction to RSS and its XML schema. In addition, it incorporates what you get in a sample application that is easy-to-code, understand, and to extend.
    Published Date : 10/Sep/2010
    Validate XML over DTD
    Published Date : 15/Feb/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top