Brian Schaffner, the author of this article, is the Deputy director of Fujitsu consulting firm. He provides architectural, design and development support to Fujitsu's technical consulting companies.
In this article, we'll use a simple Web form that lists some of the XML files in a directory. We then select a file from this directory and send it to another Web form, which populates some text fields with the selected XML file.
Sample XML
Our sample XML document contains a set of elements that contain basic information, as shown in listing a:
Listing A:sample.xml
<Sample>
<name>john doe</name>
<address>900 N. Michigan ave</address>
<City>Chicago</City>
<State>IL</State>
<Zip>60614</Zip>
<Phone>630-555-5555</Phone>
</Sample>
To make this article more interesting, we'll create two sample files that contain different data, as shown in listing B and listing C:
Listing B:sample2.xml
<Sample>
<name>george W. bush</name>
<address>1600 Pennsylvania Ave nw</address>
<City>Washington</City>
<State>DC</State>
<Zip>20500</Zip>
<Phone>202-456-1111</Phone>
</Sample>
Listing C:sample3.xml
<Sample>
<name>bill gates</name>
<address>1 Microsoft way</address>
<City>Redmond</City>
<State>WA</State>
<Zip>98052</Zip>
<Phone>425-882-8080</Phone>
</Sample>
Create a directory called C:\xmldocs and put all three sample files into this directory.
Web Forms
We will create two Web forms for our applications. We're going to use the first form to select the XML file and use the second form to display the XML data.
First you use Visual Studio. NET to create a new ASP.net Web application. The original form is then saved as a selectfile.aspx. If you double-click the form, the code editor will be opened.
The first thing you need to do is add the following line to the front of your code to be able to include the IO package in your application:
Usingsystem.io;
Then, locate the Page_Load () method within the code. You need to edit this method to image the code in listing D below:
Listing d:selectfile ' Spage_load () method
private void Page_Load (object sender, System.EventArgs e) {
System.IO.DirectoryInfo dir;
system.io.fileinfo[] files;
System.IO.FileInfofinfo;
System.Collections.IEnumeratorfileEnum;
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--showdata.aspx to your project. Place six text boxes in this form and name them: Txtname, txtaddress, txtcity, Txtstate, Txtzip, and Txtphone. Similarly, create six labels in the table dropdowns, and name their title bars name (name), address (addresses), City (cities), State (states), Zip (Zip code), and phone (telephone).
Double-click the ShowData form to start the Code Editor. You need to edit the Page_Load () method of the ShowData form so that it looks like the one in Listing E:
Listing E:showdata ' Spage_load () method
private void Page_Load (object sender, System.EventArgs e) {
string filename;
Xmltextreaderxmlreader;
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 also need to add the following line to the top of your code to be able to include the XML package in your application:
Usingsystem.xml;
This code is essentially the same as the code we use in the C # form example. The main difference is that we no longer use buttons to invoke this code, and we no longer need to use the OpenFileDialog control to look for XML files. The file name is provided by the Selectfile Web form, which is invoked when the ShowData form is loaded.
We use the Xmltextreaders Read () method to iterate through the XML document. With each call to read (), we will process another XML node. We simply check to make sure that the node is an element and then use the string data in the XML element node to populate the corresponding text box in our table.
Running the demo program
Once you have finished coding and compiling the demo form, you are ready to use them. Point your Web browser to the Selectfile form. When the page is loaded, it should be able to list the sample files we created earlier. By simply clicking on one of the filenames, you will see the data filled in the ShowData form.