1. XML stands for an Extensible Markup Language.
2. XML is used to store or transport the data.
3. XML stores the information inside the XML tags in an XML file.
We can read a XML file by using following ways:
1. XmlDataDocument Class
2. XmlReader Class
3. XmlTextReader Class
4. XPath with XmlDocument
Add following namespace:
using System;
using System.Data;
using System.Windows.Forms;
using System.Xml;
using System.IO;
1. XmlDataDocument Class
XmlDataDocument Class is used to read the XML file. This class is inherited from the XmlDocument class.
XmlDataDocument Class makes a link between the XML documents and data source.
Firstly, we create an XML file and name it as Students.xml.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Table>
<Students>
<Student_id>12</Student_id>
<Student_name>Ankit</Student_name>
</Students>
<Students>
<Student_id>24</Student_id>
<Student_name>Ashish</Student_name>
</Students>
</Table>
C# Code:
private void btn_Read(object sender, EventArgs e)
{
XmlDataDocument xdd = new XmlDataDocument();
XmlNodeList xnl;
int i = 0;
string s = null;
FileStream fs = new FileStream("Student.xml", FileMode.Open, FileAccess.Read);
xdd.Load(fs);
xnl = xdd.GetElementsByTagName("Students");
for (i = 0; i <= xnl.Count - 1; i++)
{
xnl[i].ChildNodes.Item(0).InnerText.Trim();
s = xnl[i].ChildNodes.Item(0).InnerText.Trim() + " " + xnl[i].ChildNodes.Item(1).InnerText.Trim() + " " + xnl[i].ChildNodes.Item(2).InnerText.Trim(); MessageBox.Show (s);
}
}
}
2. XmlReader Class
XMLReader class can also be used to read the data from XML file. The XMLReader class uses less amount of memory.
In C#
private void btn_Read(object sender, EventArgs e)
{
String xn =
"<?xml version='1.0'?>
<Student>
<Student_id>011</Student_id>
<Student_name>Ankita</Student_name>
</Student>";
XmlReader xr = XmlReader.Create(new StringReader(xn));
while (xr.Read())
{
switch (xr.NodeType)
{
case XmlNodeType.Element:
listBox1.Items.Add("<" + xr.Name + ">");
break;
case XmlNodeType.Text:
listBox1.Items.Add(xr.Value);
break;
case XmlNodeType.EndElement:
listBox1.Items.Add("");
break;
}
}
}
3. XmlTextReader Class
XmlTextReader class is also used to read the data from XML file. This class allows custom parsing and tokenizing of XML data. This class restricts usage when we access the data from the backward XML tag. It provide a fast access when we are accessing a data from a forward XML tag.
XML File content Student.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Class>
<Student>
<Student_id>1</Student_id>
<Student_name>Asha</Student_name>
</Student>
<Student>
<Studentt_id>2</Student_id>
<Student_name>Amit</Student_name>
</Student>
</Class>
In C#
private void btn_Read(object sender, EventArgs e)
{
XmlTextReader xm = new XmlTextReader("D:\\Student.xml");
while (xr.Read())
{
switch (xr.NodeType)
{
case XmlNodeType.Element:
listBox1.Items.Add("<" + xr.Name + ">");
break;
case XmlNodeType.Text:
listBox1.Items.Add(xr.Value);
break;
case XmlNodeType.EndElement:
listBox1.Items.Add("");
break;
}
}
}
4. XPath with XmlDocument
This is use to read the data from XML file. XPath method is defines inside the XML Document Object Model (DOM). This method selects a single element or an element which matches specific criteria.
XML File content Student.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Class>
<Student>
<Student_id>1</Student_id>
<Student_name>Asha</Student_name>
</Student>
<Student>
<Studentt_id>2</Student_id>
<Student_name>Amit</Student_name>
</Student>
</Class>
In C#
private void btn_Read(object sender, EventArgs e)
{
XmlDocument xd = new XmlDocument();
xd.Load("D:\\Student.xml");
XmlNodeList xn= xd.DocumentElement.SelectNodes("/Class/Student");
string student_id = "", student_name = "";
foreach (XmlNode n in xn)
{
student_id = n.SelectSingleNode("Student_id").InnerText;
student_name = n.SelectSingleNode("Student_name").InnerText;
MessageBox.Show(student_id + " " + student_name + " ");
}
}
}
0 Comment(s)