Click here to Skip to main content
11,935,655 members (61,108 online)
Click here to Skip to main content
Add your own
alternative version


72 bookmarked

Reading an XML file using .NET

, 31 Aug 2003
Rate this:
Please Sign up or sign in to vote.
An article on how to read an xml file into an ASP.NET page


In this article one will see how we can read and XML file it our ASP.NET application, this trick is use full for making custom configuration files for your application or just reading data stored in an xml file.


This code uses the XmlTextReader object to read the disk file into the XmlDocument object. the XmlTextReader object has very similar functionality to the StreamReader and BinaryReader objects, except that it is specifically designed to read XML file. The XmlTextReader object also has other XML-specific features. For example the WhitespaceHandling property setting in the code tells it not to create nodes for extra whitespace in the XML file.

The code uses the DocumentElement property of the XmlDocument object to find the node at the root of the tree representation of the XML document. After that, we just recursively call the procedure that adds information about the node to the ListBox control.

The code also deals with attributes. Attribues nodes are not included in the ChildNodes collection of a node in the XmlDocument object. Instead, you can use the Attributes property of the XmlNode object to get a collection of attribute nodes only. The code uses an XmlNamedNodeMap object to hold this collection; this object can hold an arbitrary collection of XmlNode objects of any type.

Code listing

  private void btnLoad_Click(object sender, System.EventArgs e)
    XmlTextReader reader = new XmlTextReader(

    reader.WhitespaceHandling = WhitespaceHandling.None;
    XmlDocument xmlDoc = new XmlDocument();
    //Load the file into the XmlDocument
    //Close off the connection to the file.
    //Add and item representing the document to the listbox
    lbNodes.Items.Add("XML Document");
    //Find the root nede, and add it togather with its childeren
    XmlNode xnod = xmlDoc.DocumentElement;

  private void AddWithChildren(XmlNode xnod, Int32 intLevel)
    //Adds a node to the ListBox, togather with its children.
    //intLevel controls the depth of indenting
    XmlNode xnodWorking;
    String strIndent = new string(' ',2 * intLevel);
    //Get the value of the node (if any)
    string strValue = (string) xnod.Value;
    if(strValue != null)
      strValue = " : " + strValue;
    //Add the node details to the ListBox
    lbNodes.Items.Add(strIndent + xnod.Name + strValue);

    //For an element node, retrive the attributes
    if (xnod.NodeType == XmlNodeType.Element)
      XmlNamedNodeMap mapAttributes = xnod.Attributes;
      //Add the attributes to the ListBox
      foreach(XmlNode xnodAttribute in mapAttributes)
        lbNodes.Items.Add(strIndent + " " + xnodAttribute.Name +
          " : " + xnodAttribute.Value);
      //If there are any child node, call this procedrue recursively
        xnodWorking = xnod.FirstChild;
        while (xnodWorking != null)
          AddWithChildren(xnodWorking, intLevel +1);
          xnodWorking = xnodWorking.NextSibling;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Tahir Nasir
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
nicachipal10-Jul-10 8:19
membernicachipal10-Jul-10 8:19 
GeneralProblem when parsing a remote file Pin
persquarefeet19-Mar-09 1:45
memberpersquarefeet19-Mar-09 1:45 
GeneralGreat Article Pin
guineapig23144-Nov-08 7:34
memberguineapig23144-Nov-08 7:34 
GeneralConvert java to c# Pin
mayuri12317-Jun-08 3:18
membermayuri12317-Jun-08 3:18 
GeneralThanks... Pin
CuneyQ14-Jun-07 4:58
memberCuneyQ14-Jun-07 4:58 
GeneralGOOD ONE Pin
tkjbbs1-Jun-07 22:35
membertkjbbs1-Jun-07 22:35 
Generalproblem in using x-path Pin
susree23-Sep-03 3:52
membersusree23-Sep-03 3:52 
QuestionUse the Read method? Pin
ajoshi3-Sep-03 1:39
memberajoshi3-Sep-03 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 1 Sep 2003
Article Copyright 2003 by Tahir Nasir
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid