| Advertise Here |
DEVELOPER CHANNEL |
Using XML to build an ASP+ config.web EditorBy Adam S. Cartwright
Building our EditorNow 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. XmlTextReaderOur 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 FileTo 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:
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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Jump to : Top Of Page or HOME |