The XmlReader and XmlTextReader classes are defined in the System.XML namespace.
The XmlTextReader class is derived from the XmlReader class. The XmlTextReader class can be used to read XML documents. The read function of this document reads the document until the end of its nodes.
In this article, I will show you how to use the XmlTextReader class to read an XML document and write data to the console.
Adding namspace Reference
Since XML classes are defined in the System.XML namespace the first thing you need to do is to add the System.XML reference to the project.
using System.Xml;
Open an XML Document
The constructor of the XmlTextReader class opens an XML file. In this sample, I used an XML file called xmltest.xml in the C\temp directory. You can download the attached file.
// Open an XML file
XmlTextReader reader = new XmlTextReader("C:\\temp\\xmltest.xml");
Reading Data
The Read method of the XmlTextReader class reads the data.
while ( reader.Read() )
{
Console.WriteLine(reader.Name);
}
Souce Code: Attachment readxml1.cs 1 KB, xmltest.xml 1 KB
namespace ReadXML
{
using System;
using System.Xml;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
}
public static int Main(string[] args)
{
try
{
// Open an XML file
XmlTextReader reader = new XmlTextReader("C:\\temp\\xmltest.xml");
while ( reader.Read() )
{
Console.WriteLine(reader.Name);
}
}
catch (Exception e)
{
Console.WriteLine ("Exception: {0}", e.ToString());
}
return 0;
}
}
}

Ibuomi AbiboeditedPosted Dec 12, 2010, 5:46 PMEdited Dec 12, 2010, 5:47 PM
Hi Mahesh, I have a question regarding navigating to a listbox item in C#. I have written code which populates a listbox from an xml file in xaml (thus bound to it). However I want to navigate to the selected item in the listbox using code in c#. I can navigate through the xml document without issues but how do I call the specific item that is selected in the listbox? For example, the listbox has items populated from the xml at node 1 and node 2 respectively: "Coffee" {i.e node1}, "Cream" {i.e node2}. By selecting 'Coffee', how can I specify this in the C# code? For more info here is part of the xaml code: <ListBox x:Name="lbRecipe2" Height="auto" Width="150" BorderBrush="Blue" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ElementName=lbRecipe, Path=SelectedItem, Mode=OneWay}" SelectionChanged="lbRecipe2_SelectionChanged" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Height="auto"> <TextBlock FontWeight="Bold" FontSize="12" Text="{Binding XPath=name}" /> <TextBlock FontSize="11" Text="{Binding XPath=summary}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> and here is part of the c# code {note the lbrecipe2. which is incomplete): private void AddButton_Click(object sender, RoutedEventArgs e) { if (lbRecipe2. == "") { } } Any help is greatly appreciated. Thanks IB
Waqas RasheedPosted Aug 2, 2009, 9:10 AM
I am first time in this forum thanks to Admin
hfsdjgshg jhsgfslfsPosted Dec 14, 2007, 4:26 PM
hi i have problem in the read part . my project is write to xml file and display to the many text box .i try your program bout i can not solve it . so plz help me
williams alexPosted Jul 24, 2007, 1:17 AM
reading a xml file in .net
williams alexPosted Jul 24, 2007, 1:16 AM
wat is the use of ~operator in XML
gazale malekPosted Jun 20, 2007, 10:15 AM
Hi, I want to read a specific data from an existing XML file and write it in my Excel(csv) file. which program should I use to do this for me? thanks