Populating a Data Grid from a Persisted XML File

Populating a Data Grid from a Persisted XML File

Just to prove that the XML file we wrote contains enough information to fully describe our data, we will populate a data grid from the SqlXML.xml file from the last example. Create a new Windows project named ReadFromXML, and then follow these steps:

  1. Add a data grid, and name it dgDataGrid. Add a button named btnPopulate, and set its Text property to "&Populate from XML." Set the form's Text property to "Read from XML."

  2. Right-click the data grid, and select Auto Format. In the Auto Format dialog box, select Colorful 1 as the new style. We might as well add a bit of glitz because it's so easy to do. Click OK to format the data grid.

  3. Now add the following code to the btnPopulate button's Click event handler. We create new DataSet, FileStream, and XmlTextReader objects. The SqlXML.xml file we created from the last example will then be read into the data set. Finally, we bind the data set to the data grid.

    Private Sub btnPopulate_Click( ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPopulate.Click Dim dsDataSet As New DataSet() 'Set the file path and name. Dim sFilename As String = "C:\SqlXML.xml" 'Create a FileStream object with the file path and name. Dim fsFileStream As New System.IO.FileStream _ (sFilename, System.IO.FileMode.Open) 'Create a new XmlTextReader object with the FileStream. Dim XmlTReader As _ New System.Xml.XmlTextReader(fsFileStream) 'Read the XML and schema into the DataSet 'and close the reader. dsDataSet.ReadXml(XmlTReader) XmlTReader.Close() With dgDataGrid .CaptionText = "Populated from XML" .DataSource = dsDataSet .AllowSorting = True .AlternatingBackColor = System.Drawing.Color.Bisque .SetDataBinding(dsDataSet, "Categories") End WithEnd Sub

Run the Program

Run the program, and then click the Populate From XML button. Remember that the file contains not only the data from the two tables but also an inline schema as well. The schema is read first and sets up the columns in the data grid. Then the XML file populates the columns with data. Because the relationships are fully described in the schema, the child records can also be displayed, as you can see in Figure 11-19.

Figure 11-19

The data grid populated from our XML file.

Expand the records, and then select the CategoriesProducts link. All of the child Products records will be displayed, just as though we read the data from the database. This example clearly illustrates just how powerful text-based XML is. From the single text file, all of the data, plus the metadata contained in the schema, is enough to reconstitute the data grid. You now see how a text-based XML file can be passed through a firewall on a Web-based server to construct a data set. Of course, this data set can be edited and then passed back to the source if you want.

How the Program Works

Let's take a look at the code in a little more detail. First a new data set is created. The name and location of our Sqlxml.xml file is placed in the sFileName string variable. A FileStream object is used to connect to our text file and open it from disk.

Dim dsDataSet As New DataSet() ' Set the file path and name. Dim sFilename As String = "C:\SqlXML.xml" ' Create a FileStream object with the file path and name. Dim fsFileStream As New System.IO.FileStream _ (sFilename, System.IO.FileMode.Open)

The XmlTextReader class provides a fast parser that enforces rules such as that the XML file must be well-formed XML. Because our file was originally created from the data set in our last program, we know that the file will be well formed.

' Create a new XmlTextReader object with the FileStream. Dim XmlTReader As _ New System.Xml.XmlTextReader(fsFileStream)

We are now ready to read the file. Use the ReadXml method of the data set to read an XML document that includes both a schema and data. To read the data from an XML document that contains only data into a data set, use the ReadXml method. To read just the schema from an XML document, use the ReadXmlSchema method of the data set.

If an inline schema is specified as in our file, the inline schema is used to extend the existing relational structure prior to loading the data. If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised. If no inline schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. But because we don't want any nasty surprises, it's easy to include an inline schema as we did. So the XML file is read and then the XmlTextReader object is closed because all the data is now transferred into the data set.

' Read the XML and schema into the DataSet ' and close the reader. dsDataSet.ReadXml(XmlTReader) XmlTReader.Close()

The rest of the code is familiar, so I won't review it here. I simply wanted to show you how powerful XML is and also how easily XML can be manipulated with the built-in .NET class framework.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net