Adding HTML table to datagridview using HTML Agility pack

c# datagridview html html-agility-pack parsing

Question

I writing simple app for parsing HTML table to datagridview using the help of HTML Agility pack. but when I run the code it throw me an error "This row already belongs to this table"

I need to parse simple HTML table like below

<html>
<head>
</head>
<body>
<table>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
   <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
</table>
</body>
</html>

this is my code

private void button1_Click(object sender, EventArgs e)
    {
        var htmlCode = richTextBox1.Text.Trim();
        var doc = new HtmlDocument();
        doc.LoadHtml(htmlCode);

        dt = new DataTable();
        dt.Columns.Add("Company", typeof (string));
        dt.Columns.Add("Contact", typeof (string));
        dt.Columns.Add("Country", typeof (string));

        var count = 0;


        foreach (var table in doc.DocumentNode.SelectNodes("//table"))
        {
            foreach (var row in table.SelectNodes("//tr"))
            {
                var dr = dt.NewRow();

                var i = 0;
                foreach (var cell in row.SelectNodes("//td"))
                {


                    dr["Company"] = cell.InnerText.Replace("&nbsp;", "");
                    dr["Contact"] = cell.InnerText.Replace("&nbsp;", "");
                    dr["Country"] = cell.InnerText.Replace("&nbsp;", "");

                }


                dt.Rows.Add(dr);
            }


            grid.DataSource = dt;
        }
    }

I need a simple output on datagridview like this

enter image description here

How can I do this with HTML Agility pack ?

Accepted Answer

Move the following line

dt.Rows.Add(dr);

outside the foreach loop over the table cells. You try to add the same row multiple times to the DataTable.

int i = 0;
foreach (var cell in row.SelectNodes("//td"))
{
    dr[i++] = cell.InnerText;
}
dt.Rows.Add(dr);


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why