Cache the DataSet as an XML file
Author: Aadil
This article will show how to manually cache data on the server in an xml file without using the Cache object.
In ASP.NET there is an object called Cache that you can use to store objects that can be accessed by multiple users. This object stores the contained data in the memory of the web server and is quire reliable because at peak levels when the server is utilizing lot of memory then ASP.NET will automatically removes objects contained in this object and the web server can utilize this memory.
We may need sometimes a caching mechanism that can be used to persist the data to the file system and not in the web server’s memory like the ASP.NET’s Cache object. In this case we can use the DataSet object’s built in capabilities that allow us to load the DataSet from an xml file and save a DataSet in an xml file.
What I’ve done in this sample example is that I am getting a DataSet from a function and binding this with a DataGrid control. The function looks for an xml file on the same path where it is stored and if it finds one, it loads the DataSet from this file and of no file is found (if this is the first time this function is called) then it fetches the data and stores it in the xml file.
The ASP.NET web page code is as follows
The code for the code behind file is