In this article we will take advantage of the XML classes available to us to persist the GridView control that we talked about in our first article in this series. The two classes we utilize in our code are XmlTextWriter and XmlTextReader.
Figure 1 - The Editable GridView with Persistence.If you've played with .NET a bit, you may have noticed Microsoft's hard push to utilize XML throughout the architecture. In this article we will take advantage of the XML classes available to us to persist the GridView control that we talked about in our first article in this series. The two classes we utilize in our code are XmlTextWriter and XmlTextReader. These classes enable us to save the information in a grid into an XML file and read an XML file into the grid.
The XmlTextWriter class has several useful functions that make creating an XML file less tedious. Below is a table of the methods we take advantage of for writing out the GridView.
XmlTextWriter methods
Description
Table 1 - XmlTextWriter Methods used in the GridView.The XmlReader class enables us to read the XML files into our code. It also has many convenient methods and properties for walking through the XML nodes and extracting data from the XML document. Below are the properties and methods we utilize for reading our GridView:
Figure 2 - UML Design of the Persistent GridView reverse engineered using the WithClass 2000 .In our example, we save all of our GridView information in an XML file including column names, column widths and cell information. The way we store grid cells and their contents is similar to how HTML stores table information. We nest the collection of column nodes inside a row node. The column node contains the text, background color, and foreground color of the cell. Below is a partial snapshot of the XML file we are going to create from the GridView shown in Figure 1.
Figure 3 - Snapshot of XML File created by the GridView Save Method
Note that the XML file stores color information inside of attributes as aRGB numbers in integer format. This is why you see these strange negative numbers after all of the color attributes.
Now let's see how we create this file using the GridPersister class. Listing 1 below is the Write method inside of this class that produces the XML file shown in Figure 3. The code first loops through the grids column headers and writes them out to the xml file. Then it loops through rows and columns in the grid and writes out the nodes representing the contents of each cell of the grid.
Listing 1 - Write method to write the XML file
public
To read the file created by the Write method of the GridPersister we have created a Read method. This is a bit more complex than the Write method because it needs to determine which node we are reading and extract the information. The code for Reading the XML looks like a series of nested switch statements because as we read each node, we need to parse the nested set of nodes inside. Below is the code for reading the GridView XML information.
Listing 2 - Reading the XML GridView File
Listing 3 - Opening the GridView from a Windows Form
private
Pro WPF: Windows Presentation Foundation in .NET 3.0