0

I've imported an Excel spreadsheet into a DataGridView. I Need the option to export it to an XML File for a job to run on other applications. problem with saving the file in a specific path

This is the button I use to export:

  private void button1_Click(object sender, EventArgs e)
                    {
                        DataSet ds = (DataSet)dataGridView1.DataSource;
                        SaveFilalog sfd = new SaveFilalog();
                        sfd.Filter = "SHEET1|*.xml";
                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                ds.Tables[0].WriteXml(sfd.FileName);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex);
                            }
                        }
                    }
Uzarsef
  • 13
  • 8
  • 1
    Just answer same question last week : https://stackoverflow.com/questions/61602250/export-datagridview-to-xml-file-c-sharp/61617371#61617371 – jdweng May 09 '20 at 13:23
  • Check [Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=netcore-3.1) for full functionality regarding C# DataTables. (ds.Tables[0] is a data type of DataTable). – Hex May 09 '20 at 13:49

1 Answers1

1

Try to use this:

using (Stream xmlFileStream = sfd.OpenFile())
{
    ds.Tables[0].WriteXml(xmlFileStream);
}
Scrappy Coco
  • 564
  • 2
  • 6
  • Doesn't work i want to mention that i need to take data directly from DGV instead of from a table – Uzarsef May 09 '20 at 18:29