We help IT Professionals succeed at work.
Get Started

C#: HtmlAgilityPack getting elements using Xpath

trevor1940 asked
Last Modified: 2020-01-02
I'm trying to use HtmlAgilityPack to Travers some  HTML Test.html

Under each b-post will be a single picture, a group of pictures in a slide show or a video I'm struggling to extract the elements like Title for  each post &  img src

  class Program
        static void Main(string[] args)
            Console.WriteLine("Hello World!");
            string Root = @"H:\TopTotty\CarrieLaChance\onlyfans\Vids\";
            string CarrieOF = Root + "Test.html"; // "CarrieOF.html";
            var htmlDoc = new HtmlDocument();

                var bPostNodes = htmlDoc.DocumentNode.SelectNodes(".//div[contains(@class,'b-post')]");

            string Title = "";
            string VidSrc = "";
            string Poster = "";
            foreach (var divNodes in bPostNodes)
                //Title = divNodes.SelectSingleNode(".//span").Attributes["title"].Value;
                    if (divNodes.SelectSingleNode(".//span").Attributes["title"] != null)
                        Title = divNodes.SelectSingleNode(".//span").Attributes["title"].Value;

                    // /html/body/main/div/div/div/div/div/div/div/div/div[1]/div[3]/div[1]/figure/div/div[2]/video/source
                    if (divNodes.SelectSingleNode(".//video/source").Attributes["src"].Value != null)

                        VidSrc = divNodes.SelectSingleNode(".//video/source").Attributes["src"].Value;
                        Uri uri = new Uri(VidSrc);

                        string LocalFile = Root + "\\" + System.IO.Path.GetFileName(uri.LocalPath);
                        if (File.Exists(LocalFile))
                            Console.WriteLine("Title  {0} , {1} ", Title, LocalFile);
                        if (divNodes.SelectSingleNode(".//video").Attributes["poster"].Value != null)
                            Poster = divNodes.SelectSingleNode(".//video").Attributes["poster"].Value;
                            Console.WriteLine("poster {0}", Poster);

                    //  Single image
                    Console.WriteLine("Title  {0} Before  Single image", Title);
                    // /html/body/div/main/div/div/div[2]/div/div[2]/div[5]/div[3]/div[1]
                    if (divNodes.SelectSingleNode(".//div[starts-with(@class,'post_img_block'])") != null)

                        // /html/body/div/main/div/div/div[2]/div/div[2]/div[5]/div[3]/div[1]/div/img
                        var imgNode = divNodes.SelectSingleNode(".//div[starts-with(@class,'post_img_block')]");
                        string ImgSrc = imgNode.SelectSingleNode(".//img").Attributes["src"].Value;
                        Console.WriteLine("Title   {0} , Image src: {1} ", Title, ImgSrc);



                    Console.WriteLine("Title  {0} Before  Slideshow", Title);
                    if (divNodes.SelectSingleNode(".//div[starts-with(@class,'swiper'])") != null)
                        var SlideNode = divNodes.SelectSingleNode(".//div[starts-with(@class,'swiper-slide')]");
                        var Slides = SlideNode.SelectNodes(".//img");
                        foreach (var Slide in Slides)
                        string ImgSrc = Slide.Attributes["src"].Value;
                        Console.WriteLine("Title in Slideshow {0} , Image src: {1} ", Title, ImgSrc);


                catch (Exception)

                Console.WriteLine("I'm Done");


Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE