Counting how many columns and rows in table of html file
How can we find how many columns and rows are there in the html file ? How can we count that how many td t开发者_如何转开发ags are there in the html file ?
Use the HTML Agility pack to parse the HTML, then query it for the number of <TR>
tags for the number of rows in a table.
For the <TD>
, use the first row and get the number of those. Check if there are any colspan
attributes and add the value of each - 1, to get the number of columns in the table.
For example, to get the number of rows:
HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
// Assuming only one table in the file
int colums = doc.DocumentElement.SelectNodes("//tr").Count();
There isn't a way in HTML to count the <TD>
rows/columns I'm afraid. You're best bet would be to get a program like Notepad++ and literally "search" for <TD
and see how many results appear. Or you could go down the Javascript route - but that's a different kettle of fish ;)
精彩评论