|
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)
{
try
{
XmlTextReader reader = new XmlTextReader ( "C:\\CREATEXMLDocument.xml" );
while (reader.Read())
{
reader.MoveToElement();
lstReadXML.Items.Add( "XmlTextReader Properties Test" );
lstReadXML.Items.Add( "===================" );
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.
|