DataTable dt = new DataTable();
foreach (DataGridViewColumn headerCell in dataGridView1.Columns)
{
dt.Columns.Add(headerCell.HeaderText);
}
foreach (DataGridViewRow item in dataGridView1.Rows)
{
DataRow dr = dt.NewRow();
for (int i = 0; i < dt.Columns.Count; i++)
{
dr[0] = item.Cells[i].Value.ToString();
}
dt.Rows.Add(dr);
}
dt.WriteXml("data.xml");