Getting Top N XML Nodes values using XPath expression.

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 437
This example shows how to select Top N nodes of the specific name from an XML document.
We can use XPath expression like this /Names/Name[position() <= 4]
Here,we will fetch top 4 nodes values.
using System.Xml;        
string str_list = "<Names><Name>Vishal</Name><Name>Vinod</Name><Name>Rajesh</Name><Name>Nitin</Name><Name>Rutu</Name><Name>Bhagyshree</Name><Name>Dharmesh</Name></Names>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(str_list);

XmlNodeList xnList = xml.SelectNodes("/Names/Name[position() <= 4]");
foreach (XmlNode xn in xnList)
{
Response.Write(xn.InnerText + "<br/>");
}


Output:-
Vishal
Vinod
Rajesh
Nitin

Comments or Responses

Login to post response