Populate ASP forms with XML data

It's possible to populate an XML form using ASP.NET. Here's how.

This article originally appeared as an XML e-newsletter.

By Brian Schaffner

Do you know how to populate a form using ASP.NET? 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
  <Name>John Doe</Name>
  <Address>900 N. Michigan Ave</Address>

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

  <Name>George W. Bush</Name>
  <Address>1600 Pennsylvania Ave NW</Address>

Listing C: sample3.xml
  <Name>Bill Gates</Name>
  <Address>1 Microsoft Way</Address>

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: <br><br>\n");

  while (fileEnum.MoveNext()) {
    finfo = (FileInfo) fileEnum.Current;
    Response.Write("<a href=\"ShowData.aspx?filename=" + finfo.Name + "\">" + finfo.Name + "</a><br>\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();
        case "Address":
          txtAddress.Text = xmlReader.ReadString();
        case "City":
          txtCity.Text = xmlReader.ReadString();
        case "State":
          txtState.Text = xmlReader.ReadString();
        case "Zip":
          txtZip.Text = xmlReader.ReadString();
        case "Phone":
          txtPhone.Text = xmlReader.ReadString();

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.

Editor's Picks

Free Newsletters, In your Inbox