Introduction to custom server controls

XML data handling

As I mentioned on the previous page this class will not be discussed in detail as this is pretty basic XML2DataSet code.

First off we'll add the class that will allow data to be retrieved from the XML file – let's call it GetGuestbookEntries and be sure to add public and static encapsulation to it:

public static DataTable GetGuestbookEntries()
{
    try
    {
        DataSet ds = new DataSet();
        FileStream fs = new FileStream( HttpContext.Current.Server.MapPath("Guestbook.xml") , FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        ds.ReadXml(fs, XmlReadMode.Auto);
        fs.Close();
        return ds.Tables[0];
    }
    catch (Exception exc)
    {
        HttpContext.Current.Trace.Warn("XmlEngine", "Error in GetGuestbookEntries", exc);
        return null;
    }
}

You should be familiar with the DataSet methods called ReadXml and WriteXml . We will use both of these in this class, and in this particular method we'll use ReadXml to read in the XML data from the XML file, and automatically populate a DataSet .
I added the try-catch block to prevent the application from crashing even though an exception might be thrown. The exception is caught and added to the page trace, so that the page developer can enable tracing and thus see the exception that is thrown.

Now onto the second and final method in the XmlEngine class; the InsertGuestbookEntry method:

public static bool InsertGuestbookEntry(string name, string email, string message, string rating)
{
    try
    {
        if (name == string.Empty)
            name = "Anonymous";
        name = HttpContext.Current.Server.HtmlEncode(name);
        email = HttpContext.Current.Server.HtmlEncode(name);
        message = HttpContext.Current.Server.HtmlEncode(name);
           
        DataTable dt = GetGuestbookEntries();
        DataRow dr = dt.NewRow();
        dr["name"] = name;
        dr["email"] = email;
        dr["message"] = message;
        dr["rating"] = rating;
        dt.Rows.Add(dr);
        FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("Guestbook.xml"), FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
        dt.DataSet.WriteXml(fs, XmlWriteMode.WriteSchema);
        fs.Close();
    }
    catch (Exception exc)
    {
        HttpContext.Current.Trace.Warn("XmlEngine", "Error in InsertGuestbookEntry", exc);
        return false;
    }
    return true;
}

This method is a Boolean method and that is because that way we can know whether or not it was executed successfully. It also takes 4 parameters – the values from the entry submission form (GuestbookForm control). All the data population and so on here is basic stuff and is self-explaining.

Ok, now that we are done with the XML handling let's get cracking on the exciting part, namely the custom server controls…

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous