Presents your XML E-NEWSLETTER for February 26, 2003 <-------------------------------------------> POPULATE A C# FORM WITH XML DATA The latest set of development tools from Microsoft includes the .NET Framework and C#. With these new technologies come new ways of working with XML data. Let's look at how to process XML data with C# and the .NET Framework. THE SAMPLE DATA We'll start the process by defining a sample form built in C#. The form is very simple and contains 12 items--six labels and six text boxes. These form components should have the properties shown in TABLE A: Table A: Sample form component properties Component Name Text Value txtName txtAddress txtCity txtState txtZip txtPhone lblName Name: lblAddress Address: lblCity City: lblState State: lblZip Zip: lblPhone Phone: We'll lay out these components in a standard configuration. On the left side of the form will be the six label controls. To the right of each label will be the associated text box. Given this form, we can create some sample XML data that might populate the form. Our XML document contains a root node, , and six child nodes, which will contain the actual data for the form. LISTING A shows the sample XML document: Listing A: sample.xml John Doe
900 N. Michigan Ave
Chicago IL 60614 630-555-5555
FILLING THE FORM Now that we have the sample data and form, we need to glue the two together. We'll start by modifying our form. The first thing we'll need is an OpenFileDialog component, which we'll use to find the XML file. Next, we'll add a button control. We'll label the button "Load." Here's what we want to happen: 1. Click the Load button. 2. Locate the sample.xml document using the OpenFileDialog. 3. Open the XML document. 4. Parse the data and populate the form. LOCK AND LOAD Double-click the Load button to open the code editor for the button1_Click() method. LISTING B shows the button1_Click() method, as we have modified it. In addition, you will need to add "using System.Xml;" to the top of your form's code. This line will include the Microsoft .NET XML components in your project: Listing B: The button1_Click() method private void button1_Click(object sender, System.EventArgs e) { string filename; XmlTextReader xmlReader; filename = "sample.xml"; openFileDialog1.FileName = filename; openFileDialog1.ShowDialog(); if (openFileDialog1.FileName != null) filename = openFileDialog1.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; } } } } If you follow the code, you'll see that we declare a couple of variables for use in the method. The first is filename--a string that we use to store the name of the file that contains the XML document. The second is xmlReader--an instance of the XmlTextReader class that we use to parse the XML document. The OpenFileDialog component is used to browse the user's computer for a specific XML document. Once the dialog runs, it will contain the name of the file the user selected in its FileName property. We assign the value of this property to our local filename variable. Next, we instantiate xmlReader and point it at the file we located earlier. The xmlReader's Read() method indicates whether we can continue processing the XML document. We use it to provide the criteria for an iteration that continues until we've processed the entire document. With each iteration, the xmlReader will provide us with an XML node for processing. Since we are only interested in some specific elements, we've added a little more logic. We test the NodeType property to see if the current node is an element. If so, then we use a switch construct to determine which element we have. Depending on the element name, we assign the value from the XML document to one of our text boxes. Brian Schaffner is a senior consultant for Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's Telcom360 group. ----------------------------------------