C# 循环通过<;td>;元素并定义行/列
我有一个HTMLC# 循环通过<;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%
元素的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