Monday, December 04, 2006 #

XML File Interface

An article on how to quickly create a Windows Forms interface to maintain data in an XML file

Platforms: Microsoft .NET 2.0, Visual Studio 2005

Technologies: Windows Forms, Data Binding, XML, XSD, DataSet

Download Source Here

Recently, I ported an application from VB 6 to .NET 2.0. The original application contained 10 XML files that were used to maintain configuration information such as background colors of different documents and font sizes. On average, each file contained about 20 fields, so I was not looking forward to the idea of building 10 new forms with 200 fields. So I started searching for an easy way to incorporate existing XML files into a .NET application. Luckily, there was a way.

Here was my plan: Take an XML file, generate an XSD schema, then use the schema to create a strongly-typed dataset in Visual Studio 2005 using MSDatasetGenerator. Once I have the dataset, and it appears in the Data Source window, drag all the fields to a Windows Form. The next step is to use the dataset's ReadXML, WriteXML methods to load and save the data to and from my original XML file.

Here is a walk-through of the process using one of my original XML files:

<?xml version="1.0" standalone="yes"?>
<
Xls
>
<Format TitleFontSize="12" TitleFontName="Arial" TitleBold="true" TitleItalic="false" TitleColor="Black" TitleBackgroundColor="White" CommentsFontSize="10" CommentsFontName="Arial" CommentsBold="false" CommentsItalic="false" CommentsColor="Black" CommentsBackgroundColor="White" GridHeaderFontSize="8" GridHeaderFontName="Arial" GridHeaderBold="true" GridHeaderItalic="false" GridHeaderColor="Black" GridHeaderBackgroundColor="White" GridDetailFontSize="8" GridDetailFontName="Arial" GridDetailBold="false" GridDetailItalic="false" GridDetailColor="Black" GridDetailBackgroundColor="White" FootnoteFontSize="7" FootnoteFontName="Arial" FootnoteBold="false" FootnoteItalic="false" FootnoteColor="Black" FootnoteBackgroundColor="White" EmbedImages="true" ColumnWidthPixels="64"
/>
<
Xls>

It’s nothing fancy. But as you can see, there are quite a few fields. Manually coding an interface to maintain this file would be a long, monotonous process. But, luckily, we can use the power of Visual Studio 2005 and Data Source window to do the bulk of the coding for us. So fire up your Visual Studio 2005, create a new Windows Forms project (C# or VB.NET), and let's give it a try.

The first step is to add the XML file to our project. Once the file is a part of the project, you need to open its Properties in order to set the Build Action to Content and Copy to Output Directory to Copy If newer. This will force the file to be copied to the output folder while you are debugging the application in Visual Studio. It will also ensure that if you use the Setup Project to package our application, the file will be a part of the installation. See Figure 1.

Next, you are going to generate an XSD schema using Visual Studio 2005. Double-click on the XML file, so you can see its content in the XML editor. You’ll discover a new XML menu between Debug and Data in the Visual Studio main menu strip. Click on the XML menu, then choose Generate Schema. Visual Studio generates a schema, also known as an XSD file, based on the XML file provided. See Figure 2.

Once the schema file is visible in the editor, save it in the file system and include it in the project.

The next step is to generate a strongly-typed dataset. To do this, open the Properties of the XSD file and then type "MSDataSetGenerator" in the Custom Tool field of the Property window. Click Save. Visual Studio will now convert this XSD file to a typed dataset. By the end of this process you should see a small database image on top of your XSD's icon in the Solution Explorer. See Figure 3.

Now, you’re ready to start working on the form that will be used to maintain information in the XML file. If you created a new project in Visual Studio, you already should have Form1 created for you. Open this form and switch it to the design mode. 

At this point, you need to make sure the Data Sources window is visible. Just go to the Data menu in the main Visual Studio menu strip and select Show Data Source. You should see a list of available data sources in the Data Source pane, along with the dataset we created earlier. See Figure 4 .

Click the datatable icon (a table with a cylinder) and then choose the Details Mode from the drop-down menu. This will ensure that the designer lays out the fields as labels and text boxes when you drag the datasource to the form in the next step. If you used the XML file supplied at the beginning of this article, the datatable name should be Format—which corresponds to the element name in our XML document. See Figure 5 .

Drag the Format datatable from the Data Source window to the form. You’ll notice a few changes: The fields are now laid out on the form and three new objects appear at the bottom of the form—xls, formatBindingSource, and formatBindingNavigator.

Hint: Notice how Visual Studio split the labels and CommentsFontName became Comments Font Name. The designer logic intelligently splits phrases based on capitalization. So ideally, you want to have your xml file populated with attributes and elements that use proper case. This will save you time because you won't have to reformat your labels on the form.

Out of scope: formatBindingNavigator allows us—among other things—to traverse multiple records in a binding source. Since, in this particular example, you are using an XML file to store only one record of information, you are going to delete the formatBindingSource component and delete the navigator control that was added by Visual Studio at the top of the form. If your XML file were to store multiple records of information, you would want to keep these two components on the form.

The next step is the wire loading and saving of the XML file. In the form's Load event, add the following code:

xls.ReadXml(System.IO.Path.Combine(Environment.CurrentDirectory, "XMLFile1.xml"));

In the form's FormClosing event, add the following code:

xls.WriteXml(System.IO.Path.Combine(Environment.CurrentDirectory, "XMLFile1.xml"));

See Figure 6 for a complete code listing.

And that’s it. The application is ready! Press F5 to run the project. Notice how the form gets populated with data from the original XML file. When you close the form, all information is saved back to the XML file. Pretty neat, huh!

posted @ Monday, December 04, 2006 8:52 AM | Feedback (31286)

Saturday, December 02, 2006 #

Back to consulting!

After three years at CCA Global Partners I decided to go back to a full-time consulting. I am excited about my next project, which is supposted to be an Internet-enabled application written in C# with a heavy use of Windows Forms and Web Services.

posted @ Saturday, December 02, 2006 8:41 AM | Feedback (10)