ARTICLE

Reading XML Files

Posted by Mahesh Chand Articles | XML March 28, 2001
In this article, I will show you how to use XmlTextReader class to read an XML document and write data to the console.
Reader Level:
Download Files:
 

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.

In this article, I will show you how to use 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, so 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 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;
}
}
}

Login to add your contents and source code to this article
post comment
     

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

Posted by Ibuomi Abibo Dec 12, 2010

I am first time in this forum
thanks to Admin

Posted by Waqas Rasheed Aug 02, 2009

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

Posted by hfsdjgshg jhsgfslfs Dec 14, 2007

Where did you see ~ operator?

Posted by Mahesh Chand Jul 24, 2007

reading a xml file in .net

Posted by williams alex Jul 24, 2007
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts