To Retrieve Node Value using HtmlAgilityPack with help of XPath

HTML Node Value using XPath


XPath, the XML Path Language, is a query language for selecting nodes from an XML document. The given below code illustrates to extract XPath using HtmlAgilityPack and webclient on the fly.


You need to add the reference of HtmlAgilityPack, I've used version

You can refer to download the .dll




using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using HtmlAgilityPack;

using System.Net;

using System.IO;


namespace DescendantUsingXPath


   public static class Program


        static void Main(string[] args)


            //WebClient object

            WebClient x = new WebClient();


            //Convert given url data to bytearray using DownloadData()

            byte[] byteArray = x.DownloadData(new Uri(""));


            //Convert Byte Array into Stram

            Stream stream = new MemoryStream(byteArray);


            //Create new object of HtmlAgilityPack.HtmlDocument

            HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();


            //To load stream into html object




            //To get the value from Given XPath

            HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode(@"/html/body/div[4]/div[2]/div/div/h1/a");


            string strValue = node.InnerText;







Source: The given below URL is being pass into DownloadData() method



Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now