Uzarsef
Uzarsef

Reputation: 13

Save a DataTable in XML C#

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);
                            }
                        }
                    }

Upvotes: 0

Views: 195

Answers (1)

Scrappy Coco
Scrappy Coco

Reputation: 564

Try to use this:

using (Stream xmlFileStream = sfd.OpenFile())
{
    ds.Tables[0].WriteXml(xmlFileStream);
}

Upvotes: 1

Related Questions