TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

How To Select Required Number Of Nodes From Top or Bottom Using XPATH - C#,VB.NET

This post explains how you can access a specified number of required XML elements using the XPATH expression,top n elements ,bottom n elements etc. Same sample is given in both C# and VB.NET.

In this post I am explaining how you can access only the specified number of required XML elements using the XPATH expression.Same sample is given in both C# and VB.NET.

Consdier the same XML sample file which I have used for my other sample codes,

<?xml version="1.0" encoding="utf-8"?>
<Employees>
  <Employee ID="E001">
    <Name>Sam</Name>
    <DOB>10/10/1960</DOB>
   </Employee>
  <Employee ID="E002">
    <Name>Peter</Name>
    <DOB>10/10/1980</DOB>
  </Employee>
  <Employee ID="E003">
    <Name>Thomas</Name>
    <DOB>10/01/1970</DOB>    
  </Employee>
</Employees> 

In the below given sample it is accessing the Employee elements which are at position above level 1 in the selected XML Element order.Similarly you can access first n element,last n elements or any of that sort according to the position.

C#

class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDoc = new XmlDocument();

            //Hardcoded path is the xml file path.Set it to your xml file path.
            xmlDoc.Load(@"E:\Demos\XmlParsersUnleashed\XmlParsersUnleashed\Employees.xml");
          
            // Call the method which parses and displays the elements as per the xpath expression
            GetElementsByPosition(xmlDoc);
        }

        /// <summary>
        /// Get Xml elements by their position in xml element hierarchy and display the result
        /// </summary>
        /// <param name="xmlDoc"></param>
        private static void GetElementsByPosition(XmlDocument xmlDoc)
        {
            //Selects the Employee elements from EMployees parent node where order of the 
//Employee element in in XML is greater than 1. XmlNodeList employeeNodeList = xmlDoc.SelectNodes("/Employees/Employee[position() > 1]"); foreach (XmlNode xmlNode in employeeNodeList) { Console.WriteLine("Name :" + xmlNode["Name"].InnerText); } Console.Read(); } }

VB.NET

Class Program
    Private Shared Sub Main(args As String())
        Dim xmlDoc As New XmlDocument()

        'Hardcoded path is the xml file path.Set it to your xml file path.
        xmlDoc.Load("E:\Demos\XmlParsersUnleashed\XmlParsersUnleashed\Employees.xml")
   
        ' Call the method which parses and displays the elements as per the xpath expression
        GetElementsByPosition(xmlDoc)
    End Sub

    ''' <summary>
    ''' Get Xml elements by their position in xml element hierarchy and display the result
    ''' </summary>
    ''' <param name="xmlDoc"></param>
    Private Shared Sub GetElementsByPosition(xmlDoc As XmlDocument)
        'Selects the Employee elements from EMployees parent node where order of the 
'employee element in in XML is greater than 1. Dim employeeNodeList As XmlNodeList = xmlDoc.SelectNodes("/Employees/Employee[position() > 1]") For Each xmlNode As XmlNode In employeeNodeList Console.WriteLine("Name :" + xmlNode("Name").InnerText) Next Console.Read() End Sub End Class

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading