Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Creating XML Trees With The XmlTextWriter and XmlDocument Objects
Dynamically create well-formed XML documents in your ASP.NET scripts

| Spending Time In The Library |

The XmlTextWriter object can best be considered as a counterpart to the XmlTextReader object, allowing you to perform the reverse function. The next example shows you how:


<%@ Page Language="C#" Debug="true" %>
<%@ import  namespace="System.Xml"%>
<html>
<head>
<script runat="server">
void Page_Load() {

// initialize a XmlTextWriter object
XmlTextWriter objXmlWriter = null;

// location to the XML file to write
String strXmlFile = "E:/Inetpub/wwwroot/xml/library.xml";

// start the "try" block
try {

objXmlWriter = new XmlTextWriter (strXmlFile , null);

// start writing the XML document
objXmlWriter.WriteStartDocument(false);

// start with the root element
objXmlWriter.WriteStartElement("library");

// first child element
objXmlWriter.WriteStartElement("book", null);

// add an attribute
objXmlWriter.WriteAttributeString("bkid","MFRE001");

// some text
objXmlWriter.WriteElementString("title", "XML and PHP");

// back to the root element
// closing each element written above
// one at a time
objXmlWriter.WriteEndElement();
objXmlWriter.WriteEndElement();

// flush the object and write the
// XML data to the file
objXmlWriter.Flush();

} catch (XmlException e) {

output.Text = "An XML Exception occurred: " + e.Message;

} catch (Exception e) {

output.Text = "A General Exception occurred: " + e.Message;

} finally {

// Close the XMLWriter object
if(objXmlWriter != null) {
objXmlWriter.Close();
}
}    
}
</script>
</head>
<body>
<asp:label id="output" runat="server" text="It's all done!" />
</body>
</html>


Run this example in your browser, and you might see something like this:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
A General Exception occurred: Access to the path "E:/Inetpub/wwwroot/xml/library.xml" is denied.
').'
'

Don't despair - this is just a matter of giving appropriate permissions on the target directory to the user. But which user?

According to the official ASP.NET documentation, the "ASP.NET V1 RTM now runs using a less privileged Windows account - registered as the ASPNET account on a local machine" (http://www.asp.net/security.aspx?tabindex=0&tabid=1).

So, all this means that you need to locate the "ASPNET" user in your user management control panel and give the corresponding user account permission to write to the target directory. Once that's done, run the example again, and you will now see a more cheerful message:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
It's all done.
').'
'

In reality, the action takes place elsewhere...

Navigate to the location specified in the script and look for a file called "library.xml". This is what it should look like.

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
<?xml version="1.0" standalone="no"?><library><book bkid="MFRE001"><title>XML and PHP</title></book></library>
').'
'

At first glance, this is very unattractive to the naked eye. I will show you later how this can be formatted to look a little nicer. First, though, it's time for a quick anatomy lesson on the code above.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback