C# 函数获取旧变量(HtmlAlityPack)
当我尝试使用foreach时,几乎没有从HTML获取文本的函数。文本对于每次迭代都是相同的,但是当我添加断点并检查应该在foreach变量中的文本时,它已经改变了。以下是代码:C# 函数获取旧变量(HtmlAlityPack),c#,.net,html,html-agility-pack,C#,.net,Html,Html Agility Pack,当我尝试使用foreach时,几乎没有从HTML获取文本的函数。文本对于每次迭代都是相同的,但是当我添加断点并检查应该在foreach变量中的文本时,它已经改变了。以下是代码: foreach (HtmlNode nodes in listingNode.ChildNodes) { i++; int row = dataGridView1.Rows.Add();
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
}
}
断点的局部变量(迭代10次后)
橙色线条显示出了问题所在。(查看代码,然后查看屏幕截图)(节点应始终与节点相同)开始时XPath可能有点棘手,当您要从子节点中选择时,必须添加一个“.”(点),否则XPath将始终从顶部节点中选择 所以 变成
nodes.SelectSingleNode(".//div[@class='bmlistt']").Attributes
也许您需要通过grid中的单元格进行Iterate,而不是在row=dataGridView1.Rows.Add()中使用相同的单元格;更改行时,单元格必须始终相同。在这里也没什么大不了的。
nodes.SelectSingleNode(".//div[@class='bmlistt']").Attributes