TechyMedia

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

NAVIGATION - SEARCH

How To Select An XML Node Element Values According To A Specific Attribute Value - C#,VB.NET

By this article I presents sample code to select individual XML element value from an XML file according to a specific attribute value provided.This sample uses Xpath expression to select the elements.

In this article I explain how to select individual XML element value from an XML file according to a specific attribute value provided,using Xpath expression.

Consider the XML file below,

<?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> 

Below samples will help you for accessing the Employee node's ineternal elements according to the ID attribute provided.The Xpath expression "/Employees/Employee[@ID='E002']" is used to fetch the Elements inside Employee element node whose ID attribute value is EOO2

C#.NET

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");
            GetSingleNodeByAttributeValue(xmlDoc);  
        }

        /// <summary>
        /// Performs the parsing and displys the element values
        /// </summary>      
        private static void GetSingleNodeByAttributeValue(XmlDocument xmlDoc)
        {
            //execute the xpath expression "/Employees/Employee[@ID='E002']" which selects
            //the Employee element with attribute ID's value 'E002'
            XmlNode employeeNode = xmlDoc.SelectSingleNode("/Employees/Employee[@ID='E002']");
            Console.WriteLine("Name :" + employeeNode["Name"].InnerText);
            Console.WriteLine("Date of Birth :" + employeeNode["DOB"].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")

               GetSingleNodeByAttributeValue(xmlDoc) 

        End Sub
 

        ''' <summary>

        ''' Performs the parsing and displys the element values

        ''' </summary>       

        Private Shared Sub GetSingleNodeByAttributeValue(xmlDoc As XmlDocument)

               'execute the xpath expression "/Employees/Employee[@ID='E002']" which selects 

               'the Employee element with attribute ID's value 'E002'

               Dim employeeNode As XmlNode = xmlDoc.SelectSingleNode("/Employees/Employee[@ID='E002']")

               Console.WriteLine("Name :" + employeeNode("Name").InnerText)

               Console.WriteLine("Date of Birth :" + employeeNode("DOB").InnerText)

               Console.Read()

        End Sub

End Class

 

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading