Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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# 循环通过<;td>;元素并定义行/列_C#_Html_Xpath_Matrix_Html Agility Pack - Fatal编程技术网

C# 循环通过<;td>;元素并定义行/列

C# 循环通过<;td>;元素并定义行/列,c#,html,xpath,matrix,html-agility-pack,C#,Html,Xpath,Matrix,Html Agility Pack,我有一个HTML元素的HtmlNodeCollection,这些元素是我使用HTMLAgilityPack从表中收集的。通常,我只会选择表中的元素,并循环遍历元素,但不幸的是,开始标记是通过JavaScript生成的,不是从服务器呈现的。我无法控制HTML的呈现方式。因此,我求助于从这个XPATH查询中获取HtmlNodeCollection: HtmlNode table = htmlDoc.DocumentNode.SelectSingleNode("//table[@width='100%

我有一个HTML
元素的HtmlNodeCollection,这些元素是我使用HTMLAgilityPack从表中收集的。通常,我只会选择表中的
元素,并循环遍历
元素,但不幸的是,
开始标记是通过JavaScript生成的,不是从服务器呈现的。我无法控制HTML的呈现方式。因此,我求助于从这个XPATH查询中获取HtmlNodeCollection:

HtmlNode table = htmlDoc.DocumentNode.SelectSingleNode("//table[@width='100%' and @cellpadding='1' and @cellspacing='1' and @border='0']");
HtmlNodeCollection tds = table.SelectNodes(".//td[@align and string-length(@width)=0]"); // only select td elements that have the align attribute and don't have a width attribute
表中有六列和任意数量的行。我想处理每一行,并将列解析为中间数据结构。我有获取每个“行”和“列”的代码,但不太正确:

int cols = 6; // six columns
int rows = tds.Count / cols;

// loop through the rows
for (int row = 1; row <= rows; row++)
{
    for (int col = 0; col < cols; col++)
    {
        HtmlNode td = tds[col * row]; // get the associated td element from the column index * row index
        MessageBox.Show(td.InnerHtml + "\n" + td.InnerText);
    }
}
int cols=6;//六列
int rows=tds.Count/cols;
//环行

对于(int row=1;row,在纸上画出一个网格后,我清楚地知道我缺少了什么。我需要将列索引添加到与当前行相乘的列数中,如下所示:

for (int row = 0; row < rows; row++)
{
    for (int col = 0; col < cols; col++)
    {
        HtmlNode td = tds[col + cols * row]; // get the associated td element from the column index * row index
        MessageBox.Show(td.InnerHtml + "\n" + td.InnerText);
    }
}
for(int row=0;row