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
本文介绍了一个使用C#实现的简单应用程序,该程序能够将数据从XML文件导入到DataGridView,并将DataGridView中的数据导出到XML文件。通过示例代码展示了如何使用DataSet进行XML文件的数据读写操作。
2032

被折叠的 条评论
为什么被折叠?



