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

Search XML Files:

DEVELOPER CHANNEL
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

Find a Web Host With:
CGI Capabilities
Active Server Pages
Windows Hosting
Unix Hosting
Dedicated Servers
     
 Advanced Search
 Be a Commerce Partner















IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

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 
- Persisting Data in Your Windows Mobile Device
- Generating Reports and Statistics in PHP
- Leveraging Silverlight 2 with SQL Server and XML
- Enable WPF/Windows Forms Interoperability with WPF Commands
- Getting Started with XForms in OpenOffice

Jump to : Top Of Page or HOME




JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES