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

Search XML Files:

XML Files RSS Feed XML Files Updates

 

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 4 of 4

Moving to the DOM

Now that we're using the XmlTextReader object, what if we wanted to create a DOM object out of it? Using the same example as above, let's add just a few lines to introduce DOM functionality.


<%@ 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);

            // Create the XmlDocument from the XmlTextReader
            XmlDocument xmldocument = new XmlDocument();
            xmldocument.Load (reader);
            Console.WriteLine ("File: {0} sucessfully read!", myDocument);
            Console.WriteLine ("DOM object loaded successfully ...");
            Console.WriteLine ();
            Console.WriteLine ("XML Document Contents:");
            xmldocument.Save(writer);
	  }
        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>

The lines in red above are the only chances necessary to create a DOM object! Now that we have our DOM object, we can move on and pass it to our client side XML data-island. But first, we'll make one last modification to the page. Instead of writing out to the page as we have been, we'll change the content type of the page to "text/xml" and write the XML data directly to the response stream. This will allow us to load directly into our data island. The modified code now looks like this:


<%@ 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();
	Response.ContentType = "text/xml";
    	Response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>");
	Response.Write(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
            reader = new XmlTextReader (myDocument);

            // Create the XmlDocument from the XmlTextReader
            XmlDocument xmldocument = new XmlDocument();
            xmldocument.Load (reader);
            xmldocument.Save(writer);
	        }
        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{
            // Destroy the reader object
            if (reader != null)
                reader.Close();
	   }
        return writer.ToString();
	}
} 
</script>

Notice the use of the System.Web.HttpResponse object above. We can simply use standard ASP Response statements to do so. In order to stay compliant we need to add the <?xml version='1.0'> tag to our XML document. That way this file can be used by more than just our editor tool.

Now, the last step is to build our HTML file with the XML Data Island to load into. For now we'll use a simple HTML file as such:


<html>
<head><title>ASP+ Config.Web Editor</title>
<script>
function showXML(){
  alert(myData.xml);
}
</script></head>
<body>
<h1>ASP+ Config.Web Ediotr</h1>
<xml id="myData" src="sample3.aspx"></xml>
<button id="myButton" onClick="showXML();">Show XML</button>
</body>
</html>

Here our XML Data Island is identified using the <xml id="mydata" src=",,,"></xml> structure. Notice how the source is pointing to our previous example "sample3.aspx". You can see how flexible this method is since we can dynamically build the XML on the server and load it into a client side DOM object. By clicking on the button the content of the Data Island is shown so that we can verify that we have successfully loaded the XML. Now we're ready to build our editing tool that will allow us to modify the XML document and save it back to the server.

Summary

Today we've examined the ASP+ config.web file and learned about it's capabilities. We then learned that using the XmlTextReader object on the server we can easily read XML documents onto the server. We also looked at how to send that XML data down to the client as XML. Using this technique we can build rich applications that build XML on the server and leverage client capabilities to handle the resulting XML data.

In our next lesson we will take our editor to the next step by creating a client side editing environment in Internet Explorer using the XML Data Island and data binding features of IE. We'll discover how to use the DOM on the client side to modify the XML data, and return the XML data back to the server.

Supporting Materials

A zip file containing the code from this article is available for download: source.zip (6 kb)


< Previous Page Page 4 of 4

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 DevX XML Content 
- Making XQuery Control Structures Work for You
- Sharpening Your Axis with Visual Basic 9
- Generating Generic XForms for OpenOffice
- Using Templates for Concurrent XSLT Transformations
- Relational Database Integration with RDF/OWL

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