|
download source code
Introduction
Sometimes you don't have a database table to work with, but you still want your page to be dynamic
in terms of the data it presents to the user. Meaning, you don't want to have to modify code in
order to make the page display new information or information in a different way. You would prefer
to have some aspects of the data that is presented on the page abstracted and separated from the page itself.
In the days of classic ASP, I took advantage of XML files stored on the server to accomplish this.
These XML files would store both the data and the metadata that the page needed to create a dynamic
display. It would get read in by the page, parsed up and VBScript code would then make determinations
about what to do or how to display it. If I needed to make a change, I would just modify the XML and
without changing code the page would display the new or modified data.
In ASP.NET there are even more opportunities you can take advantage of using this approach. For
instance, you can store the information used by the page in a DataSet that has been serialized out
to XML. This saved XML contains the schema for the table structure(s) as well as the data. The
code behind your aspx page can simply read the XML in from disk using the ReadXml method of the
DataSet object. From there, you can work with the DataSet object the same as if you had filled
it via a SqlAdapter object that pulled the information from a SQL Server database. In addition,
you could make use of ASP.NET's extremely useful caching features and make it dependant upon
modifications to the XML file residing on the disk. Then instead of having to read it in from
disk each time, you would only do it once, and until the file on disk changed, ASP.NET would store
the DataSet in the server cache.
Applications
Of course, in most cases you'd ideally like to have a database table, but there are certain situations
where this sort of method is a practical alternative. One such use is if you have a page
that requires only a few or even one set of data elements. It may not be practical to create
a table in your database to store that few number of rows. In fact, you may have your Database
Administrator become obstinate about it and question your judgment about the matter, especially
in the case where only one row is needed.
Another use might be where the requirements for the
page display change so rapidly that it does not yet make sense to create a table and then ask
the DBA to alter it over and over again in an attempt to keep up with the demands of your demanding
users. This puts you more in control and also provides a quick and flexible approach to getting
changes out quickly until the chaos settles down. After that, you can quickly and easily take
your DataSet and model and table (or tables) from that to get the data into a table in a database.
What is great about this is very little code would have to be modified since the code can work
with the DataSet object in the same way as it did before.
A third use for this may be if you are hosting your site on an ISP server and they do not provide
a database for you to work with. In this case, you are stuck with a dilemma and this provides an
excellent alterative. Especially if the amount of data is not very large.
Working Example
Here is an example of the output from a simple page that pulls in product information from a
DataSet stored on disk and displays it:
Here is a look at the XML itself that is stored on disk. It represents a serialized DataSet
containing a table called "Products" that has four rows.
Here is a snippet of the VB.NET code of the page that reads it, sorts and filters it, and
displays the data using a DataList. Notice, that it is also making use of the Cache object
to store the DataSet in memory. This cache would expire if the physical contents of the
file stored on disk were to change. In that instance, the next execution of the page would
read in the new DataSet from disk again.
Try
' Assume we are not going to show anything
DataList1.Visible = False
' See if the product list is in cache
dsProducts = CType(Cache(sCacheName), System.Data.DataSet)
If dsProducts Is Nothing Then
' Get the path to the XML file containing the product list
sPath = ConfigurationSettings.AppSettings("ProductListFile")
' Translate it to a physical file name
sPath = Server.MapPath(sPath)
' Check to make sure that the the actual file exists
If File.Exists(sPath) Then
' Populate the DataSet with the XML contents of the file
dsProducts = New DataSet
fs = New FileStream(sPath, FileMode.Open, FileAccess.Read)
xtr = New System.Xml.XmlTextReader(fs)
dsProducts.ReadXml(xtr, XmlReadMode.ReadSchema)
xtr.Close()
fs.Close()
' Populate cache with the DataSet and make its expiration
' dependant upon a change to the file
Cache.Insert(sCacheName, dsProducts, New CacheDependency(sPath))
Else
Throw New System.Exception("Product File does not exist!")
End If
End If
' Now let us display the products if any
If dsProducts.Tables("Products").Rows.Count > 0 Then
dv = dsProducts.Tables(0).DefaultView
' Sort by name
dv.Sort = "Name"
' Display only those that are ok to show
dv.RowFilter = "Display = 1"
If dv.Count > 0 Then
DataList1.DataSource = dv
DataList1.DataBind()
DataList1.Visible = True
End If
End If
Catch ex As System.Exception
Throw ex
End Try
An additional add-on to this approach would be to create a page that allows editing of the data
stored in the XML. You simply would read it in as before, manipulate the data stored in the
DataSet, and then write the XML back out to the same location. This would save having to
manually update the file itself each time there is a data change. It would also provide
the ability for someone that is not familiar with XML, such as a content person, or non-technical
website administrator, to update the data through a more pleasing user interface and effectively
change what gets displayed on the product page shown above.
You can download a zip file containing the complete working example from
here.
Conclusion
To summarize, this approach is simply an alternative to using a DataSet populated from
information stored in a database. It goes without saying that wherever possible, it
makes much better sense to have your data stored in a table in a database. However,
sometimes this is either not desirable or not possible. In these cases, using the above
technique makes for a sensible solution.
|