The XmlReader and XmlTextReader classes are defined in the System.XML namespace.The XmlTextReader class is derived from XmlReader class. The XmlTextReader class can be used to read the XML documents. The read function of this document reads the document until end of its nodes.


 


 


Example: The following example will show how the C# program will read the xml file.


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.Xml;


 


namespace _CSharpApplication


{


  public partial class Form11 : Form


    {


    public Form11()


        {


    InitializeComponent();


        }


    private void button1_Click( object sender, EventArgs e)


        { //Read XML Document


      try


            {


        // Open an XML file : Read Any XML that you have


                XmlTextReader reader = new XmlTextReader ( "C:\\CREATEXMLDocument.xml" );


        while (reader.Read())


                { // Move to first element


            reader.MoveToElement();


            lstReadXML.Items.Add( "XmlTextReader Properties Test" );


            lstReadXML.Items.Add( "===================" );


            // Read Element's Properties


            lstReadXML.Items.Add( "Name:" + reader.Name);


            lstReadXML.Items.Add( "Base URI:" + reader.BaseURI);


            lstReadXML.Items.Add( "Local Name:" + reader.LocalName);


            lstReadXML.Items.Add( "Attribute Count:" + reader.AttributeCount.ToString());


            lstReadXML.Items.Add( "Depth:" +reader.Depth.ToString());


            lstReadXML.Items.Add( "Line Number:" + reader.LineNumber.ToString());


            lstReadXML.Items.Add( "Node Type:" + reader.NodeType.ToString());


            lstReadXML.Items.Add( "Attribute Count:" + reader.Value.ToString());


                }


            }


        catch ( Exception ex)


            {


        MessageBox .Show ( "Exception: {0}" , ex.ToString());


            }


 


        }


    }


}


 


Output: The following screen will be displayed when run the above program.


 





 


Clicking On the ‘ Read XML “ button the following screen will be displayed.


 


                    

Copyright © 2010 VisualBuilder. All rights reserved