Saving, Showing, and Reading XML

This section gives you a hands-on opportunity to process XML documents that you create yourself, have Visual Studio .NET generate, or obtain from a third party. Initially, the section shows how to persist data as XML. Then, you learn how to examine a saved XML document file. The section closes with a sample that populates a dataset based on an XML document.

Saving XML

Start the setup for the XML processing samples by creating a typed dataset named dsShippers . As you have learned, a typed dataset is an XML schema that works jointly with the .NET Framework to offer capabilities through class instances based on the DataSet class and the XML schema. With Server Explorer open and the Design view of Form1 for your XMLSamples project showing, drag the Shippers table from the Northwind database connection to Form1 . After Visual Studio .NET creates the OleDbDataAdapter1 object pointing at the Shippers table, separately select the object, right-click it, and choose Generate Dataset. In the Generate Dataset dialog box, type dsShippers as the name of the new dataset and click OK. These steps add a new file named dsShippers.xsd to Solution Explorer and a variable named DsShippers1 to the tray below Form1 that points at the XML schema in dsShippers.xsd. The dsShippers.xsd file is a typed dataset based on an XML schema for a local copy of the Shippers table from the Northwind database. The local copy is a DataTable object.

One common application requirement is to persist the contents of one or more tables from a database file locally. This requirement can exist because a connection to a data source is unreliable or because a user is traveling to a location where access to the data source is not available. By saving the contents of a dataset pointing at the Northwind database to a local file in the current project, you make a snapshot of that database available even when you cannot connect to a remote data source (in this case, the Northwind database). The Form1_Load and Button1_Click procedures work together to persist the contents of a dataset pointing at the Northwind database to a local XML document. The Form1_Load procedure fills the DsShippers1 dataset with contents from the Northwind database as soon as Form1 opens. Then, when a user clicks Button1 , the Button1_Click procedure transfers the contents of the dataset via the dataset s WriteXml method to a local XML document named FromDsShippers1.xml in the root folder of the current project.

Note  

The Form1_Load procedure assigns values to the Text property for all three buttons on Form1 . These assignments help to clarify the role of each button.

 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Fill local dataset from source database OleDbDataAdapter1.Fill(DsShippers1, "Shippers") Format buttons Button1.Text = "Save XML" Button2.Text = "Show XML" Button3.Text = "Read XML" End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Write XML representation of dataset contents to a file DsShippers1.WriteXml("..\FromDsShippers1.xml") End Sub 

You might have to click the Show All Files control on the toolbar in Solution Explorer (once or twice) to make the FromDsShippers1.xml file visible. Double-clicking the file in Solution Explorer opens it in the XML Designer. The XML document s root tag has the name dsShippers , and this tag has an xmlns attribute pointing at the namespace for the dsShippers dataset. The .xsd file for the dataset defines the structure of the XML document in the FromDsShippers1.xml file.

Note  

The code sample for the Button3_Click procedure demonstrates how to assign an XML document, such as the one persisted in the Button1_Click procedure, as the source for a dataset.

Showing XML

Clicking Button2 shows as an XML document in a message box containing the contents for the data source at which the DsShippers1 dataset points. The Button2_Click procedure will fail if the Button1_Click procedure isn t invoked first because the Button2_Click procedure uses the FromDsShippers1.xml file in the project s root folder as the source for the message box it displays. However, you can generally apply the syntax in the Button2_Click procedure to any XML document file. The procedure instantiates an XmlTextReader object to make the contents of the XML document available in the xtr1 variable. Then, a Do loop iterates through the XML document nodes to find the first node that has XML markup (for example, a tag). The ReadOuterXml method detects this kind of content. In fact, the method returns the markup for the current node and all its children. This is typically the first node after the XML document declaration ” the node for the root tag. The document declaration line starts with < ?xml version= 1.0 . Figure A-4 shows the return value from the ReadOuterXML method in a message box. If you are working with long XML document files, you might need to show a document in a series of message boxes or use another approach to displaying the XML document contents. Before the Button2_Click procedure closes, it closes the XmlTextReader object at which the xtr1 variable points. This frees the object for subsequent use in another process.


Figure A-4: A message box showing the contents of a previously saved XML document
 Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Declare String and XmlTextReader Dim str1 As String Dim xtr1 As New _ System.Xml.XmlTextReader("..\FromDsShippers1.xml") Loop to first element that contains XML Do While (xtr1.Read()) str1 = xtr1.ReadOuterXml If str1 <> "" Then MsgBox(str1) End If Loop Close XmlTextReader for re-use later xtr1.Close() End Sub 

Reading XML

The Button3_Click procedure populates a dataset based on the contents of an XML document file. The Button3_Click procedure creates a FileStream object, fls1 , for an xtr1 XmlTextReader object to consume . This technique avoids passing a string directly as the argument to the XmlTextReader constructor. A dataset object can directly consume an XmlTextReader argument with its ReadXml method. The sequence of instructions from the instantiation of the FileStream object to the invocation of the ReadXml method populates the das1 dataset with the contents of the XML document at which the fls1 object points. In this case, the XML document is the XMLFile1.xml file from the Creating an Element-Centric XML Document section earlier in this appendix.

After populating the das1 dataset, the Button3_Click procedure calls the PrintValues procedure, passing the das1 dataset and a string for the name of the dataset. This called procedure displays the contents of the dataset, which reflects the contents of the XML document that serves as the source for the dataset. Essentially, the PrintValues procedure iterates through the Tables collection of the das1 dataset. Within each DataTable object, the procedure iterates through the rows and then the columns within successive rows. Figure A-5 displays the message box presented by the PrintValues procedure to confirm the transfer of the XML contents to the dataset. Notice that values appear without XML tags from the dataset.


Figure A-5: A message box showing the contents of a dataset populated from an XML document (XMLFile1.xml)
 Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click Open XML document contents into a FileStream object, read the FileStream object into an XmlTextReader object for consumption by a DataSet object Dim fls1 As New System.IO.FileStream _ ("..\XMLFile1.xml", System.IO.FileMode.Open) Dim xtr1 As New System.Xml.XmlTextReader(fls1) Dim das1 As New DataSet() das1.ReadXml(xtr1) Close XmlTextReader xtr1.Close() Print out values of each table in the DataSet using the PrintValues function PrintValues(das1, "New DataSet") End Sub Private Sub PrintValues(ByVal das1 As DataSet, _ ByVal label As String) Declare variables for looping Dim dtb1 As DataTable Dim drw1 As DataRow Dim dcl1 As DataColumn Dim str1 As String Name dataset in message box str1 = vbCr & label & vbCr Loop through tables, and then rows within tables For Each dtb1 In das1.Tables str1 &= "TableName: " & _ dtb1.TableName + vbCr For Each drw1 In dtb1.Rows For Each dcl1 In dtb1.Columns str1 &= vbTab & _ " " & drw1(dcl1).ToString() Next dcl1 str1 &= vbCr Next drw1 Next dtb1 Display message computed in the loop MsgBox(str1) End Sub 
 


Programming Microsoft Visual Basic. NET for Microsoft Access Databases
Programming Microsoft Visual Basic .NET for Microsoft Access Databases (Pro Developer)
ISBN: 0735618194
EAN: 2147483647
Year: 2006
Pages: 111
Authors: Rick Dobson

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