DataSets and XML – A Perfect Combination

 
 

Update Data in a XML file using DataSet

The code that follows updates the xml document with the most recent data in the DataSet instance. It works the same way as the above method (DeleteData) in the sense that it accepts the employee ID representing the employee record to be updated. It searches the same and updates the employee record if one exists.
private void UpdateData(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 =dvProducts.Find(“empID”);
if (rowIndex ==  – 1)
Response.Write(“This employee code does not exist”);
else
{
dataViewEmployee.RowFilter = ” Emp_ID =” + empID;
dataViewEmployee[0][“Emp_Name”] = “Joydip”;
dataSetEmployee.WriteXml(filePath,XmlWriteMode.WriteSchema);
}
}

Delete Data from XML File using a DataSet

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.

References

Please refer to the following links for further reference on this topic:–
DataGrids, DataSets, and XML Strings
Using XML in a DataSet
DataSets and Xml Representation
Smarten Up Your DataSets with XML Schema

Points to be noted

Note that if the XML is generated originally from a DataSet, using DataSet.WriteXml method, you can directly import it with DataSet.ReadXml method. On the other hand, if the XML data is in some other format, you need to read the Xml into an XmlDocument object and then write the necessary code to import the records one after the other, or use XSLT to transform the XML data into a format that can be understood by the DataSet class.
Note that when writing data to a XML file you can choose if you want to write the Schema only, Schema and Records, or just the records.  When you read the XML data you can also decide if the tables and columns should be created based on the schema, or a new schema should be created and then the data XML read, ignoring the fields that are not present in the dataset or the datatable instance.
The DataSet class is generic in the sense that it is independent of the source of the data that is populating it. This is why, unlike a DataReader, we have only one type of DataSet. Note that we have DataReaders for each type of Data Providers, i.e., each type of databases.
A point of advice: Avoid using DataSets if you only require using it as a data container, i.e., a container of data. I would recommend using a Custom Business Entity class instead that would boost the performance to a large extent due to avoidance of un-necessary boxing and un-boxing issues. Refer to my article at Passing Data through Layers that illustrates how we can build and implement a custom business entity class in lieu of a DataSet to pass data through the layers of an application.
 

Conclusion

This article has discussed how we can use DataSets and XML to perform the CRUD operations without having an underlying database like SQL Server, Oracle, etc. This approach is cost effective but can suit environments where the volume of data being handled in much less. This is not a recommended solution in environments where we require voluminous data handling as it would consume a lot of memory and hence become a nightmare. Further, the disk I/O would also be very high, hence degrading the performance of the application as a whole.  Reader’s queries and suggestions are welcome.]]>

Leave a comment

Your email address will not be published.