I am using the following query to parse html table data.
Dim q = From table In htmldoc.DocumentNode.SelectNodes("//table[@class='Seller']").Cast(Of HtmlNode)()
From row In table.SelectNodes("tr").Cast(Of HtmlNode)()
From header In row.SelectNodes("th").Cast(Of HtmlNode)()
From cell In row.SelectNodes("td").Cast(Of HtmlNode)()
Select New With {Key .Table = table.Id, Key .CellText = cell.InnerText, Key .headerText = header.InnerText}
How can i use for each loops how can to fill this into a datatable?
I would create columns first using the header data then use a nested for each loop to fill the cell data in the table, but i am not sure how to, also any suggested changes on the above LINQ query?
Note: The html page contains only one table always.
Given the following
htmland your query
you can use
GroupByorToLookupto group the objects by columns:and use this grouping to create a
DataTablewith the appropriateDataColumns:Now, for filling the
DataTable, you have to “rotate” the grouping, since each group contains the data for one column, but we want the data for each row. Let’s use a little helper methodto fill the
DataTable.And now the
DataTablewill look like this: