Advertise Here
XML Files - Extensible Markup Language Tutorials for Developers - ?
Extensible Markup Language

Search XML Files

XML Files RSS Feed XML Files Updates

 


Using XML to build an ASP+ config.web Editor

By
Adam S. Cartwright

< Previous Page Page 3 of 4 Next Page >

Building our Editor

Now that we're squared away on the config.web file, what it is and what it looks like, let's get started building our ASP+ based editor tool. In this lesson we are only going as far as loading the config.web document into a DOM object on the client. In follow-on lessons we'll parse and modify the XML data, then return it to the server and save it.

DOM vs. File System Object vs. XmlTextReader

Our task of loading the config.web file into a client side DOM object can be solved quite simply if we were to establish an XML data island on our page and load the config.web file directly into it. However, for our tool we're going to load the file on the server side first, then send it down to the client. This will allow us to add some important server side pre-processing and post-processing to our tool.

Loading the config.web file on the server side can be accomplished several ways including using either the file system object, or the XML DOM object itself, or using the new XmlTextReader object. Each method has its own advantages. For example, we may want to include some additional information to the user of our tool such as last file save date and time. This information is easily obtainable using the file system object. On the other hand, we might be storing that information ourselves, along with versioning and other data directly in the XML file. Using the DOM in this case would likely be preferable. Then again, we might be concerned about speed and performance, and the XmlTextReader object can give us all that without the overhead of the DOM object.

Loading the XML File

To become familiar with loading the XML document on the server, let's first build a page specifically for loading XML using the XmlTextReader class. This class provides direct parsing and tokenizing of XML. The object created here is not a DOM object (we'll cover that in a minute). Instead, the XmlTextReader provides fast, stream based access to the XML data without the overhead of the DOM object.

Below is our .aspx file we'll use to test loading the config.web file and for becoming more familiar with the XmlTextReader class:


<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.NewXml" %>

<script language="C#" runat=server ID=main_Script>
private void Page_Load(Object sender, EventArgs e)
{
	ReadConfigWebFile myReadConfigWebFile = new ReadConfigWebFile();
	output.InnerHtml = myReadConfigWebFile.Start(Server.MapPath("config.web"));
}

public class ReadConfigWebFile{
	private string myDocument = "";
	TextWriter writer;
	
    public string Start(String args){
	    myDocument = args;
	    StringWriter writer = new StringWriter();
	    Console.SetOut(writer);

        XmlTextReader reader = null;

        try{
            // Load the config.web file with the XmlTextReader
            Console.WriteLine ("Reading file: {0} ... please wait.", myDocument);
            reader = new XmlTextReader (myDocument);
            Console.WriteLine ("File: {0} sucessfully read!", myDocument);
	        }
        catch (Exception e){
	    // Write an error if the file was not read properly
            Console.WriteLine ("Failed reading file: {0} !", myDocument);
            Console.WriteLine ("Exception: {0}", e.ToString());
 	    }        
        finally{
	    // State we are done.
            Console.WriteLine("Loading of file: {0} is complete.", myDocument);
            
	    // Destroy the reader object
            if (reader != null)
                reader.Close();
	    }

	return writer.ToString();
    }
} 
</script>
<html>
<head></head>
<body>
<table align="left">
<tr><th><span></span></th></tr>
<tr><td><h4><xmp id="output" runat="server"/></h4></td></tr>
</table>
</body>
</html>

Let's quickly review what is being done here. First, we're importing the System.IO, System.Xml, and System.NewXml assemblies. We'll need these for reading the XML document and for communicating back to the user.

Next we have the Page_Load handler and the ReadConfigWebFile class. The Page_Load handler fires off the Start method of the ReadConfigWebFile class, passing to it the name of the configuration file we are going to read (in this case, config.web). Notice that the Page_Load handler is also going to write to the page the results of our loading.

The ReadConfigWebFile class is quite simple, but may look completely foreign to you if you're used to the ASP method of using the DOM. Here, in the Start method, we are using three main objects: myDocument, which is the name of the configuration file we passed in, the Console object that we pass messages to, and the XmlTextReader object used for reading the XML data.

Our "try" structure is going to attempt to open the XML document "config.web" simply by creating a new XmlTextReader object, passing into it the file path and name of our configuration file. Using the XmlTextReader object is that simple! If the load is successful, then processing continues to the "finally" struct, otherwise the "catch" struct will be processed where we will learn what the error message is.

Lastly, we'll return our message, which will be displayed on the screen.


< Previous Page Page 3 of 4 Next Page >

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 DevX XML Content 
- Easily Create Responsive Websites Using Bootstrap

Jump to : Top Of Page or HOME