Writing and Reading Data in a DataTable as XML in ASP.NET 2.0
Author: Aadil
In this article I’ll show you how you can use the DataTable object’s capabilities to read and write its contents as xml.
In the previous versions of ASP.NET the DataSet object was at our disposal for converting offline database data to xml format. You could use the DataSet object, populated with data from a single or more tables from a database and you could write that data in an xml file. Similarly you can read the same data from the disk file using the DataSet object’s methods.
Now in ASP.NET 2.0 most of this functionality is still present in the DataSet object but the DataTable object’s functionality has been raised too to include persisting its contents in an xml file and also for the reverse, reading the xml saved data back to construct the DataTable.
I’ll provide you with some sample code that reads a database table and fills that in the DataTable and then uses the DataTable object’s WriteXml method to save its contents to a file. The reverse process is also same, there is a method ReadXml that is similar and you can do that on your own. Here is the sample code.