0
string htmlCode = "";
using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.UserAgent, "AvoidError");
htmlCode = client.DownloadString("http://www.site.html");
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlCode);
var headers = doc.DocumentNode.SelectNodes("//tr/th");
DataTable table = new DataTable();
foreach (HtmlNode header in headers)
table.Columns.Add(header.InnerText);
foreach (var row in doc.DocumentNode.SelectNodes("//tr[td]"))
table.Rows.Add(row.SelectNodes("td").Select(td => td.InnerText).ToArray());
This example was posted in another topic on Soen. I don’t know if the data table is already filled in here and I don’t know what the table field names are.
Could explain your problem better?
– Randrade
Yes, I want to import an HTML table from a site, based on the url and preempt a datatable. Once filled, I want to use my DTO (transference object) from my application to preempt another control to show the data on the screen. I don’t know if so far in this code the datatable is filled and I don’t know how to get the names of the table fields. This is the first time I use this approach. I always use data source like Sql to get the data.
– Daniel
Did you ever test this code? Apparently it is already generating the
DataTable
.– Randrade
yes, I have tested it now and the datatable contains the rows and columns, but I cannot get the name of the table fields to implement the rest.
– Daniel
Could you post the site you are using to retrieve this data? That is, if you have no problem posting
– Randrade