How to Select Top N XML Nodes using XPath in c# ?

how to select Top N nodes of the specific name from an XML document. To select nodes from XML use method XmlNode.SelectNodes
xml file below lik this

<CustomerNames>
<Name>Srinivas</Name>
<Name>Ravi Raj</Name>
<Name>Yogi Raj</Name>
<Name>Kiran Kumar</Name>
<Name>Vasu Dev</Name>
</CustomerNames>

XmlDocument xml = new XmlDocument();
xml.LoadXml(str);  // suppose that str string contains "<Names>...</Names>"

XmlNodeList xnList = xml.SelectNodes("/CustomerNames/Name[position() <= 4]"); //suppose if you want to select only top 4 nodes.
foreach (XmlNode xn in xnList)
{
  Console.WriteLine(xn.InnerText);
}
About Author: author 4 + years of Information Technology experience in understanding and analyzing the software and hardware engineering requirements, onsite and offshore management of product development. Expert work on design and development for windows,web,mobile Expert knowledge of C#,ADO.NET, ASP.Net MVC, ENTITY FRAMEWORK, LINQ,COLLECTIONS, JAVASCRIPT,AngularJs 1.0,Angular2, Jquery,Jquery-Ajax, SQL SERVER, XAML,Windows phone 8.1 Good knowledge of WCF-Restful services,WCF, WEB API-Services ,Web- Services,MIcrosoft-Azure,JUICE UI,HTML,CSS,AJAX Read More...

Join him on Google+ | Facebook | Linkedin

No comments