DataGridView中的数据导入导出到XML文件

来源:互联网 发布:什么软件可以画图 编辑:程序博客网 时间:2024/05/16 14:17

using System.IO;  

 

 

#region   导入到XML

        private void btnLeadInToXml_Click(object sender, EventArgs e)
        {

            this.openFileDialog1.Filter = "XML文件(*.xml)|*.xml";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string filename = this.openFileDialog1.FileName;
                DataSet ds = new DataSet();
                ds.ReadXml(filename);
                this.dgvDictionary.DataSource = ds.Tables[0];

            }


        }
        #endregion


        #region   文件导出到XML文件

        private void btnExportToXml_Click(object sender, EventArgs e)
        {
            this.saveFileDialog1.FileName = DateTime.Now.ToString("yyMMddhhmmss");
            this.saveFileDialog1.Filter = "XML文件(*.xml)|*.xml";
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                DataTable dt = (DataTable)this.dgvDictionary.DataSource;
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.WriteXml(this.saveFileDialog1.FileName);
                MessageBox.Show("数据成功保存到" + this.saveFileDialog1.FileName, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion