Open and read XML file in C-Sharp

Hello everyone, In this article we will learn, How to open and read xml file in C#. Here we will use two classes to read xml file contents. XmlDocument and XmlReader.

XmlDocument Class: Used for programmatically read, modify, and remove XML in the document.

XmlReader:  Represents a reader that provides fast, non-cached, forward-only access to XML data.

Here we will create two different methods using XmlDocument and XmlReader respectively to read xml data from xml file.

Step 1:  Create an xml file naming StudentXml.xml in Drive D: (any) with following contents and save.

<Students>   <Student StudentId="1" Class="2nd">Amit</Student>   <Student StudentId="2" Class="2nd">Anil</Student>   <Student StudentId="3" Class="2nd">Rohit</Student>   <Student StudentId="4" Class="2nd">Ashu</Student> </Students>

Step 2:  Create an console application. add an new class XMLReader.cs.

Step 3:  Create an property and constructor to get file path physical location. to load xml file as follows.

public string XmlFilePath { get; set; } public XMLReader(string XmlFilePath) {     this.XmlFilePath = XmlFilePath; }

Step 1:   Create a new method for reading xml file ReadByXmlDocument() using xmlDocument class.

Add namespace using System.Xml;

public void ReadByXmlDocument() {     XmlDocument xmlDoc1 = new XmlDocument();     xmlDoc1.Load(this.XmlFilePath);     XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("Student");     if (itemNodes.Count > 0)     {         foreach (XmlElement node in itemNodes)             Console.WriteLine($"StudentId {node.Attributes["StudentId"].Value} : Class {node.Attributes["Class"].Value} : Name {node.InnerText}");     } }

Step 5:  Create a new method for reading xml file ReadByXmlReader() using xmlReader class.

public void ReadByXmlReader() {     XmlReader xmlReader = XmlReader.Create(this.XmlFilePath);     while (xmlReader.Read())     {         if (xmlReader.NodeType == XmlNodeType.Text)         {             Console.WriteLine($" : Name {xmlReader.Value}");         }         if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Student"))         {             if (xmlReader.HasAttributes)                 Console.Write($"StudentId {xmlReader.GetAttribute("StudentId")} : Class {xmlReader.GetAttribute("Class")}");         }     } }

Step 6:  Add main method to read xml file using both methods as follows.

static void Main(string[] args) {     XMLReader x = new XMLReader(@"D:\\StudentXml.xml");     Console.WriteLine("Start reading in xml file using XmlDocument.");     x.ReadByXmlDocument();     Console.WriteLine("Start reading in xml file using xmlReader.");     x.ReadByXmlReader();     Console.ReadKey(); }

Following code will read xml file in two ways using XmlDocument and xmlReader Class.

using System; using System.Xml; namespace Program {     class XMLReader     {         public string XmlFilePath { get; set; }         public XMLReader(string XmlFilePath)         {             this.XmlFilePath = XmlFilePath;         }         public void ReadByXmlDocument()         {             XmlDocument xmlDoc1 = new XmlDocument();             xmlDoc1.Load(this.XmlFilePath);             XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("Student");             if (itemNodes.Count > 0)             {                 foreach (XmlElement node in itemNodes)                     Console.WriteLine($"StudentId {node.Attributes["StudentId"].Value} : Class {node.Attributes["Class"].Value} : Name {node.InnerText}");             }         }         public void ReadByXmlReader()         {             XmlReader xmlReader = XmlReader.Create(this.XmlFilePath);             while (xmlReader.Read())             {                 if (xmlReader.NodeType == XmlNodeType.Text)                 {                     Console.WriteLine($" : Name {xmlReader.Value}");                 }                 if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Student"))                 {                     if (xmlReader.HasAttributes)                         Console.Write($"StudentId {xmlReader.GetAttribute("StudentId")} : Class {xmlReader.GetAttribute("Class")}");                 }             }         }         static void Main(string[] args)         {             XMLReader x = new XMLReader(@"D:\\StudentXml.xml");             Console.WriteLine("Start reading in xml file using XmlDocument.");             x.ReadByXmlDocument();             Console.WriteLine("Start reading in xml file using xmlReader.");             x.ReadByXmlReader();             Console.ReadKey();         }     } }

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories