<html> <head> <title>Welcome to Saurabh's GuestBook.</title> <script language="C //run the script when the Page is Loaded public void Page_Load(Object sender, EventArgs e) { // an label , its use stated later tryagain :
//the path to the Xml file which will contain all the data //modify this if you have any other file or directory mappings. //modify this if you have been directed here from Step 2 of the ReadMe file. string datafile = "db/guest.xml" ;
//try-Catch block to read from an XML file try { //make an instance to the XMLDataDocument class //this class can read from an xml file in and ordered format XmlDataDocument datadoc = new XmlDataDocument();
// Infer the DataSet schema from the XML data and load the XML Data datadoc.DataSet.ReadXml(new StreamReader(Server.MapPath(datafile)));
//Databind the first table in the Dataset to the Repeter MyDataList.DataSource = datadoc.DataSet.Tables[0].DefaultView; MyDataList.DataBind();
//free up the XML file to be used by other programs datadoc=null;
} catch(IOException ed) { // Here I am for now trying to overcome a bug in my guestbook exapmle //the Bug is that only one class can either read or write to my XML // data file at a time. //If the file is being used my some some other page (eg the guest book viewing page) // then an IOException will be thrown // So to handle such situtations what we do is that // If an IOException is thrown the page goes again to the tryagain label //and tries to write again into the xml file //this goes on till finally the resource is freed and the xml file is written to.
goto tryagain ; } catch (Exception edd) { //catch any other exceptions that occur errmess.Text="Cannot read from XML file because "+edd.ToString() ; }