Reading data from an XML file using C#

This article describes that how to read the data from XML file using C# ...


This article describes that how to read the data from XML file using C#. Before entering this concept, first of all we should know that what is XML? XML (Extensible Markup Language) is a very popular format used to store and share the data. XML stores information in a tree-based text format that allows you and me as well as computers to easily read the data.

Using the following code snippet, we can easily read the XML file date.

XmlTextReader reader = new XmlTextReader("C:\\links.xml");
while (reader.Read())
XmlNodeType nodeType = reader.NodeType;
switch (nodeType)
case XmlNodeType.Element:
Console.WriteLine("Element name is {0}", reader.Name);
if (reader.HasAttributes)
for (int i = 0; i < reader.AttributeCount; i++)
Console.WriteLine("Attribute is {0} with Value {1}: ", reader.Name, reader.Value);
case XmlNodeType.Text:
Console.WriteLine("Value is: " + reader.Value);

The above code loads an XML file called links.xml, using the while loop to look at each node, checks whether the node is an element or text, and depending on whether the node is an element or text, does something such as printing something to our console.
This article has described that how to read the data from XML file using C#.


Guest Author: tuck!13 Feb 2014

That was VERY helpful - thanks!

Author: maruthanayakam21 Feb 2014 Member Level: Silver   Points : 7

Dear All,

Please find below coding.

using System;
using System.Xml;
namespace ReadXml1
class Class1
static void Main(string[] args)

XmlTextReader textReader = new XmlTextReader("C:\\books.xml");

while (textReader.Read())

Console.WriteLine("XmlTextReader Properties Test");
Console.WriteLine("Name:" + textReader.Name);
Console.WriteLine("Base URI:" + textReader.BaseURI);
Console.WriteLine("Local Name:" + textReader.LocalName);
Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString());
Console.WriteLine("Depth:" + textReader.Depth.ToString());
Console.WriteLine("Line Number:" + textReader.LineNumber.ToString());
Console.WriteLine("Node Type:" + textReader.NodeType.ToString());
Console.WriteLine("Attribute Count:" + textReader.Value.ToString());


Author: Umesh Bhosale25 Mar 2014 Member Level: Silver   Points : 0

if your XML file is Well Formated then
dataset has method ReadXml("Filepath").

e.g. DataSet Ds = new DataSet();

Umesh bhosale

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: