Presents your
XML E-NEWSLETTER for March 12, 2003
<------------------------------------------->
POPULATE ASP FORMS WITH XML DATA
In the XML Forum, a Builder.com member asked me to demonstrate how to
populate a form using ASP.NET. Member JOLMSTEAD's request is in response to
a recent XML e-newsletter that showed how to populate a form in
Microsoft Visual C# using the .NET XML classes.
http://builder.com.com/forumdiscuss/thread_detail.jhtml?thread_id=119011&message_id=725463&fromtm=e057
For this demonstration, we'll use a simple Web form that will list some
XML files from a particular directory. Then, we'll select a file from the
directory and send it to another Web form, which will use the selected
XML file to populate some text fields.
THE SAMPLE XML
Our sample XML document contains a small set of elements containing
basic information, as shown in LISTING A.
Listing A: sample.xml
John Doe
900 N. Michigan Ave
Chicago
IL
60614
630-555-5555
To make this demonstration a little more interesting, we'll create two
more sample files that have different data in them, as shown in LISTING B
and LISTING C:
Listing B: sample2.xml
George W. Bush
1600 Pennsylvania Ave NW
Washington
DC
20500
202-456-1111
Listing C: sample3.xml
Bill Gates
1 Microsoft Way
Redmond
WA
98052
425-882-8080
Create a directory called C:\xmldocs and place all three sample files
in that directory.
WEB FORMS
We'll create two Web forms for our application. We'll use the first form
to select the XML file and the second form to display the XML data.
To begin, create a new ASP.NET Web application with Visual Studio .NET.
Save the initial form as SelectFile.aspx. If you double-click the form,
the code editor will open.
You'll first need to add the IO package to your application by adding
the following line at the top of the code:
using System.IO;
Next, locate the Page_Load() method within the code. You'll need to
edit this method to look like the code in LISTING D:
Listing D: SelectFile's Page_Load() method
private void Page_Load(object sender, System.EventArgs e) {
System.IO.DirectoryInfo dir;
System.IO.FileInfo[] files;
System.IO.FileInfo finfo;
System.Collections.IEnumerator fileEnum;
dir = new DirectoryInfo ("C:\\xmldocs");
files = dir.GetFiles ("*.xml");
fileEnum = files.GetEnumerator();
Response.Write("Please select the XML file to load:
\n");
while (fileEnum.MoveNext()) {
finfo = (FileInfo) fileEnum.Current;
Response.Write("" + finfo.Name + "
\n");
}
}
Now add a new Web form to your project, ShowData.aspx. Place six text
boxes on the form and name them: txtName, txtAddress, txtCity, txtState,
txtZip, and txtPhone. Likewise, create six labels on the form with captions
for Name, Address, City, State, Zip, and Phone.
Double-click the ShowData form to view the code editor. You'll need to
edit the ShowData form's Page_Load() method to look like LISTING E:
Listing E: ShowData's Page_Load() method
private void Page_Load(object sender, System.EventArgs e) {
string filename;
XmlTextReader xmlReader;
filename = "C:\\xmldocs\\" + Request.Params.Get("filename");
xmlReader = new XmlTextReader(filename);
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element) {
switch (xmlReader.LocalName) {
case "Name":
txtName.Text = xmlReader.ReadString();
break;
case "Address":
txtAddress.Text = xmlReader.ReadString();
break;
case "City":
txtCity.Text = xmlReader.ReadString();
break;
case "State":
txtState.Text = xmlReader.ReadString();
break;
case "Zip":
txtZip.Text = xmlReader.ReadString();
break;
case "Phone":
txtPhone.Text = xmlReader.ReadString();
break;
}
}
}
}
You'll also need to add the XML package to this form by adding the
following line to the top of the code:
using System.Xml;
This is essentially the same code we used in our C# form example. The
major differences are that we're no longer calling this code from a
button,
and we're no longer using an OpenFileDialog control to locate the XML
file. Instead, the filename is provided by the SelectFile Web form, and
this code is called when the ShowData form is loaded.
We use the XmlTextReaders Read() method to iterate through the XML
document. With each call to Read(), we process another XML node. We simply
check to make sure that the node is an element, and then populate the
appropriate text box on our form with the string data from the XML
element node.
RUNNING THE DEMO
Once you've coded and compiled the demo forms, you're ready to try them
out. Point your Web browser at the SelectFile form. When the page loads,
it should list the sample files we created earlier. Simply click one of
the file names to see the data populate the ShowData form.
Brian Schaffner is an associate director for Fujitsu Consulting. He
provides architecture, design, and development support for Fujitsu's
Technology Consulting practice.
----------------------------------------