Well when I try to use foreach with few functions for getting text from HTML. Text is always the same for every iteration, BUT when i add breakpoint and check text which should be in foreach variable, it has changed. Here is code:
foreach (HtmlNode nodes in listingNode.ChildNodes)
{
i++;
int row = dataGridView1.Rows.Add();
dataGridView1.Rows[row].Cells[0].Value = i; // ktory z kolei?
HtmlAttributeCollection imageNode = nodes.SelectSingleNode("//div[@class='bmlistt']").Attributes; // image
string node = imageNode[1].Value; // link for that image
// add adding image to row
dataGridView1.Rows[row].Cells[1].Value = null; // image
//HtmlNode artistspan = artistNode.SelectSingleNode("//span[@class='artist']");
dataGridView1.Rows[row].Cells[2].Value = nodes.SelectSingleNode("//div[@class='maintext']").SelectSingleNode("//span[@class='artist']").InnerHtml; //returns always same text
dataGridView1.Rows[row].Cells[3].Value = null; // title
dataGridView1.Rows[row].Cells[4].Value = null; // difficulties
//dataGridView1.Rows[row].Cells[5].Value = null; // Checkbox
if (i == 10)
{
i++; // breakpoint
}
}
locals from breakpoint (after 10 iterations)
https://dl.dropbox.com/u/21125662/compilation/_01055.jpg
Orange lines shows what’s wrong. (look at code, and then at screenshot) (node should be always the same as nodes )
XPath can be bit tricky at first, when you want to select from a child node you have to add one ‘.’ (dot) or your XPath will always select from the top node.
So…
becomes…