How to open and read XML file in C#

XML is a self describing language and it gives the data as well as the rules to extract what data it contains. Reading an XML file means that we are reading the information embedded in XML tags in an XML file.

C# XML Parser

how to load xml in C#

In the previous program we create an XML file and named it as products.xml. The following C# program read that file and extract the contents inside the XML tag. We can read an XML file in several ways depends on our requirement. This program read the content in Node wise . Here we are using XmlDataDocument Class to read the XML file . In this program it search the Node and its child Nodes and extract the data in child nodes.

Click here to download the input file : product.xml

How to read XML file from C#

Reading Xml with XmlReader

XmlReader opens and parses XML files. It is a faster and less memory consuming alternative. It lets you run through the XML string one element at a time, while allowing you to look at the value, and then moves on to the next XML element. It provides a lower-level abstraction over the XML file structure.

C# XML Handling

Full Source : Reading Xml with XmlReader

Reading XML with LINQ

You can read Xml file using LINQ also. The following source code shows how to read an XML file using LINQ.

Copy and paste the above XML code to a trext editor and save it as Product.XML

Source Code:

Reading XML with XmlTextReader

C# XML Handling

XmlTextReader provides direct parsing and tokenizing of XML and implements the XML specification as well as the namespaces in the XML specification from the W3C. XmlTextReader Class provides forward-only, read-only access to a stream of XML data.

Full Source : Reading Xml with XmlReader

Looking for a C# job ?

There are lot of opportunities from many reputed companies in the world. Chances are you will need to prove that you know how to work with .Net Programming Language. These C# Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of .Net Programming. Here's a comprehensive list of .Net Interview Questions, along with some of the best answers. These sample questions are framed by our experts team who trains for .Net training to give you an idea of type of questions which may be asked in interview.

Go to... C# Interview Questions

Using XPath with XmlDocument

Introduction to XML with C#

XmlDocument reads the entire XML content into memory and then allow you to navigate back and forward in it or even query the XML document using the XPath technology.

Click here to download the input file : product.xml

using System;
using System.Data;
using System.Xml;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
    public partial class Form1 : Form
        public Form1()
        private void button1_Click(object sender, EventArgs e)
            XmlDocument xmlDoc = new XmlDocument();
            XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/Table/Product");
            string proID = "", proName = "", price="";
            foreach (XmlNode node in nodeList)
                proID = node.SelectSingleNode("Product_id").InnerText;
                proName = node.SelectSingleNode("Product_name").InnerText;
                price = node.SelectSingleNode("Product_price").InnerText;
                MessageBox.Show(proID + " " + proName + " " + price);
} (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About