C#: HtmlAgilityPack getting elements using Xpath

I'm using HtmlAgilityPack to Travers some  HTML   VidsTest.html

In the code bellow I cannot get to the second video block with  <div class='post_title'>Title 2 The Wombles
Even though  the ID of "user_post_2712102" is being set Title and other variables remain at Title 1 Doctor Who

    HtmlAgilityPack.HtmlDocument HTMLdoc = new HtmlAgilityPack.HtmlDocument();
            HTMLdoc.Load(@"E:\path\to\VidsTest.html");
            var user_postDiv = HTMLdoc.DocumentNode.SelectNodes("//div[contains(@class,'user_post_')]");
            string id = "";
            string Title = "";
            string VidSrc = "";
            string Poster = "";
            string postDate = "";
            foreach (var divNodes in user_postDiv)
            {
                id = divNodes.GetAttributeValue("item_id", "").ToString();
                if (id != "")
                {
                    Console.WriteLine("id = user_post_" + id);
                
                //    postDate = divNodes.SelectSingleNode("//span[@class='local - time']").InnerHtml;
                // can't find postDate
                Title = divNodes.SelectSingleNode("//div[@class='post_title']").InnerText;

                VidSrc = divNodes.SelectSingleNode("//video/source").Attributes["src"].Value;
                Poster = divNodes.SelectSingleNode("//video").Attributes["poster"].Value;
                    Console.WriteLine("Title end div in divs {0} , {1} , {2} " + Title + Poster + VidSrc);
                }
                
            }
           Console.WriteLine("I'm Done");
            Console.ReadLine();

Open in new window

LVL 1
trevor1940Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
It's probably because when you start the xpath with //, your searching the entire structure and you're just finding the same node over and over again. You're not actually traversing the DOM or searching inside just that element.

Use .// Instead.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trevor1940Author Commented:
Hi
I didn't know "//" searched the whole document and ".//" searched from the current node

FYI
I had to capture  using if block otherwise I got unhandled exceptions

 
                    if(divNodes.SelectSingleNode(".//div[@class='post_title']") != null)
                    {
                        Title = divNodes.SelectSingleNode(".//div[@class='post_title']").InnerText;
                    }

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.