C# XML XmlReader

C# XML XmlReader C# XMLReader Example.

To Read XML in C# or VB.net we need to use System.XML namespace. Here we will see how to read XML stored at C:\ drive using C#.net and VB.net. Every developer has come across the situation to read XML file programmatically. Let’s see basic example to read XML.

XMLReader has Static Create method which opens XML file to read. You can also use XML stream received by HttpWebResponse. You can also XMLReader to read RSS or ATOM feeds.

System.XML has very rich library to deal with XML. We will see XMLReader here.

C# XmlReader Example.
using System;
using System.Xml;

namespace Csharp.XMLReader.Example
  class Program
    static void Main(string[] args)

      using (XmlReader reader = XmlReader.Create(@"C:\revenDemo.xml"))

        while (reader.Read())
          if (reader.IsStartElement())

            switch (reader.Name)
              case "tutorials":
                Console.WriteLine("Value of tutorials element is: {0}", reader.Value.Trim());

              case "examples":
                if (reader.Read())
                Console.WriteLine("Value of examples element is: {0}", reader.Value.Trim());



As you can see, we have stored XML file at C:\ HDD drive as Revendemo.xml. It contains tutorials and examples node. Below is the xml used in this example.

<?xml version="1.0" encoding="utf-8" ?>
<tutorials>Asp.net XML Reader</tutorials>
<tutorials name="xxx">Element of XML example.</tutorials>
<tutorials>XMLNode list example.</tutorials>
<tutorials>Example and tutorials.</tutorials>
<tutorials>XML c# example.</tutorials>
<tutorials>C# tutorial</tutorials>

<examples>c# dateTime</examples>
<examples>C# reade file</examples>
<examples>C# write file</examples>
<examples>C# string</examples>


VB.net XmlReader Example.
Imports System
Imports System.Xml

Namespace Csharp.XMLReader.Example
Class Program
	Private Shared Sub Main(args As String())

		Using reader As XmlReader = XmlReader.Create("C:\revenDemo.xml")

			While reader.Read()
			If reader.IsStartElement() Then

			Select Case reader.Name
			Case "tutorials"

			If reader.Read() Then
			Console.WriteLine("Value of tutorials element is: {0}", reader.Value.Trim())
			End If

			Exit Select
			Case "examples"
			If reader.Read() Then
			Console.WriteLine("Value of examples element is: {0}", reader.Value.Trim())
			End If
			Exit Select

			End Select
			End If
			End While
		End Using

	End Sub
End Class
End Namespace

Summery Here we saw how to read XML using XMLReader in C# and VB.net language.


C# foreach loopc# arraylistc# stringC# DateTime DifferenceC# Regular Expression Get Text Between BracketsC# Binary To StringC# For LoopReplace string in C#C# Break StatementC# while loopC# DateTime


My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.

I'm Satalaj.