Write for Us
The following code illustrates how we can delete data from an XML file using a DataSet. The method DeleteData accepts the employee ID as the parameter searches the employee record in the DataSet instance and deletes the record if one exists.
private void DeleteData(int empID){ string filePath = …; //Some path indicating where the xml file is stored DataSet dataSetEmployee = GetEmployeeDataSet(); DataTable dataTableEmployee = dataSetEmployee.Products.Tables[0]; DataView dataViewEmployee = new DataView(); dataViewEmployee = dataTableEmployee.DefaultView; dataViewEmployee.Sort = "Emp_ID"; int rowIndex = dataViewEmployee.Find("empID"); if (rowIndex == - 1) Response.Write("This employee code does not exist"); else { dataViewEmployee.RowFilter = " Emp_ID =" + empID; dataViewEmployee.Delete(0); dataSetEmployee.WriteXml(filePath,XmlWriteMode.WriteSchema); }}
Note that in both of these above methods (UpdateData & DeleteData) you can alternately call the SearchData method to find whether the employee record exists prior to updating or deleting the record. This would minimize a lot of code redundancy.