In this article I will explain with an example, how to select all Nodes (Elements) in XML without specifying (particular) Node Name (Tag Name) using XPath Query (Expression) with C# and VB.Net.
The XML file will be loaded into an XmlDocument and then using XPath Query (Expression), all the Nodes (Elements) in the XML will be selected.
 
 
XML
The following XML file will be used to illustrate the use of XPath with XmlDocument and how to select XML nodes by Name and Attribute value using XPath in C# and VB.Net.
You can download the following XML using the download link provided below.
 
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
    <Data>
        <EmployeeId>244186</EmployeeId>
        <Category>Temporary</Category>
        <JoiningDate>7/16/2020</JoiningDate>
        <BranchID>86022</BranchID>
        <VerificationStatus>In Progress</VerificationStatus>
        <FinanceID>39284713</FinanceID>
        <MobileNumber>333898979</MobileNumber>
    </Data>
</Employees>
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Xml;
 
VB.Net
Imports System.Xml
 
 
Using XmlDocument XPath to select all Nodes in XML without using Name in C# and VB.Net
The following example explains how to use XPath with XmlDocument and select XML nodes without specifying Node name.
The XML file is loaded into an XmlDocument and then using XPath all the nodes are fetched as XmlNodeList.
Note: Here the XPath Query //text() is used which will select all descendant Nodes with Text value and it is applicable to only Nodes which contain a Text value and do not have children.
 
Finally, a loop is executed and the InnerText value of all the selected nodes are printed.
C#
//Load the XML file in XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Xml\EmployeeData.xml");
 
//Fetch all the Nodes.
XmlNodeList nodeList = doc.SelectNodes("//text()");
 
//Loop through the Nodes.
foreach (XmlNode node in nodeList)
{
    //Fetch the Node's Name and InnerText values.
    Console.WriteLine(node.ParentNode.Name + ": " + node.InnerText);
}
 
VB.Net
'Load the XML file in XmlDocument.
Dim doc As XmlDocument = New XmlDocument
doc.Load("D:\Xml\EmployeeData.xml")
 
'Fetch all the Nodes.
Dim nodeList As XmlNodeList = doc.SelectNodes("//text()")
 
'Loop through the Nodes.
For Each node As XmlNode In nodeList
   'Fetch the Node's Name and InnerText values.
   Console.WriteLine(node.ParentNode.Name & ": " & node.InnerText)
Next
 
 
Screenshot
XPath - Select all Nodes in XML without using Tag Name with C# and VB.Net