hhh hhh -4 years ago 199
HTML Question

c# HtmlAgilityPack for on nodes array

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.

Answer Source

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].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download