Blog

Read Top 10 XMLNode

By Rahul Saxena Jul 20 2010
Read numbers of XMLNode from an XML file.
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 6.7k
  • 0

Suppose Your EmployeeRecord.xml look like

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
  <Employee>Ram</Employee>
  <Employee>Mohan</Employee>
  <Employee>Steve</Employee>
  <Employee>Allen</Employee>
  <Employee>Mike</Employee>
  <Employee>Alex</Employee>
  <Employee>Shyam</Employee>
  <Employee>Guru</Employee>
  <Employee>Aman</Employee>
  <Employee>Amit</Employee>
  <Employee>Ajit</Employee>
  <Employee>Mohit</Employee>
  <Employee>Sara</Employee>
</Employees>

And You want to read only top 10 Employee Records...

XmlDocument xmlDoc = new XmlDocument();
string FileName = Server.MapPath("EmployeeRecord.xml");
xmlDoc.Load(FileName);
XmlNodeList xnList = xmlDoc.SelectNodes("/Employees/Employee[position() <= 10]");
foreach (XmlNode xn in xnList)
{
    Response.Write(xn.InnerText);
    Response.Write("</br>");
}


By using this it will print only top 10 node of EmployeeRecord.xml file, like...


Ram
Mohan   
Steve
Allen
Mike 
Alex
Shyam
Guru
Aman
Amit