c# HtmlAgilityPack for on nodes array

arrays c# html html-agility-pack

Question

I'm using html agility pack and after I got array of nodes:

HtmlNode[] nodes = document.DocumentNode.SelectNodes("//tbody[@class='table']").ToArray();

now i want to run a for loop one each nodes[i]. I've tried this:

 for (int i = 0; i < 1; i++)
            {

                if (t == null)
                    t = new Model.Track();

                 HtmlNode[] itemText = nodes[i].SelectNodes("//td[@class='artist']").ToArray();

                for (int x = 0; x < itemText.Length; x++)
                { //doing something      }

the problem is that the itemtext array isn't focusing on nodes[i] . but brings out an array of all the ("//td[@class='artist']") in the html document. help?

Accepted Answer

Using //td[@class='artist'] will fetch all columns with artist class from your document.DocumentNode.

Using .//td[@class='artist'] (Notice the dot at the begining) will fetch all columns with artist class from the current selected node, which in your case is nodes[i].




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why