read and write xml file with dataset

Oct 18 2006 7:31 PM
could anyone helps me how to read and write dataset or datatable from and to xml file in c#.net. i have tried some technique but i get no result. my code as follow: private void btnSave_Click(object sender, System.EventArgs e) { if(dataGrid2.DataSource.GetType()==typeof(DataSet)) { DataSet ds=new DataSet(); ds=(DataSet)dataGrid2.DataSource; if(ds!=null) { saveFileDialog1.ShowDialog(); ds.WriteXml(saveFileDialog1.FileName,XmlWriteMode.WriteSchema); MessageBox.Show(this,"Save Completed"); } } else // if(dataGrid1.DataSource.GetType()==typeof(DataTable)) { DataTable dtSource=new DataTable(); dtSource=(DataTable)dataGrid2.DataSource; DataTable dtNew=new DataTable(); dtNew=dtSource.Clone(); DataSet ds=new DataSet(); ds.Tables.Add(dtNew); saveFileDialog1.ShowDialog(); ds.WriteXml(saveFileDialog1.FileName,XmlWriteMode.WriteSchema); MessageBox.Show(this,"Save Completed"); } } private void btnRead_Click(object sender, System.EventArgs e) { try { if(dataGrid1.DataSource.GetType()==typeof(DataSet)) { DataSet ds; ds=(DataSet)dataGrid1.DataSource; if(ds!=null) { dataGrid2.DataSource=ds; MessageBox.Show(this,"Save Completed"); } } else//(dataGrid1.DataSource.GetType()==typeof(DataTable)) { DataTable dt=new DataTable(); dt=(DataTable)dataGrid1.DataSource; dataGrid2.DataSource=dt; MessageBox.Show(this,"Transfer Completed"); } } catch(Exception ex) { MessageBox.Show(this,ex.Message); } } so could anyone helps to correct my code or show me other techniques. thanks in advance for helping. (new to programm)