Import data from HTML table to DataTable in C#

后端 未结 2 1562
梦如初夏
梦如初夏 2020-12-17 23:07

I wanted to import some data from HTML table (here is a link http://road2paris.com/wp-content/themes/roadtoparis/api/generated_table_august.html) and display first 16 people

2条回答
  •  时光取名叫无心
    2020-12-17 23:59

    HtmlDocument doc = new 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); // create columns from th
    // select rows with td elements 
    foreach (var row in doc.DocumentNode.SelectNodes("//tr[td]")) 
        table.Rows.Add(row.SelectNodes("td").Select(td => td.InnerText).ToArray());
    

    You'll need the HTML Agility Pack library to use this code.

提交回复
热议问题