Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 函数获取旧变量(HtmlAlityPack)_C#_.net_Html_Html Agility Pack - Fatal编程技术网

C# 函数获取旧变量(HtmlAlityPack)

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时,几乎没有从HTML获取文本的函数。文本对于每次迭代都是相同的,但是当我添加断点并检查应该在foreach变量中的文本时,它已经改变了。以下是代码:

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