12.1 Use XMLWriter to Create an XML Document
Sometimes I need to take data that is in my database and write it out to an XML
document. I heard that XMLWriter is a good way to do this. What does XMLWriter do,
and how do I create an XML document with it?
Technique
The XMLWriter provides a quick way to generate streams or files that contain XML
data. The stream is not cached; it is forward-only. The XML data that the XMLWriter
generates conforms to W3C XML 1.0 and the namespaces in XML recommendations.
With XMLWriter, you can accomplish the following:
•
Create well-formed XML.
•
Manage the output-including methods to determine the progress of the output-with
the WriteState property.
•
Flush or close the output.
•
Write multiple documents to one output stream.
•
Encode binary bytes as base64 and as binhex, and write out the resulting text.
•
Report the current namespace prefix, xml:lang, or xml:space scope.
•
Write valid names, qualified names, and name tokens.
XMLWriter has one implementation: the XMLTextWriter.
To show you how to use the XMLTextWriter, the sample code will create a data table,
allowing the user to add names to it. Then the XMLTextWriter will be used to write the
data from the data table into an XML document.
Creating the Data Table
Rather than using a DataAdapter object to create and populate the data table from live
data, the code will create the data table from scratch, and the user will add data to it. To
WriteEndElement Ends the row or column.
Flush Flushes the stream from memory.
Close Closes the string.
Steps
Open and run the Visual Basic .NET-Chapter 12 solution. From the main Web page,
click on the hyperlink with the caption How-To 12.1: Use XMLWriter to Create an XML
Document. When the page loads, you can enter a few names by entering the last and first
names and then clicking the button labeled Add to DataTable. When you have added a
few names, click the button labeled Create XML File. Using Explorer, open the file
created in C:\ called test.xml (see Figure 12.1).
1. Create a Web Form. Then place the Labels, TextBoxes, Buttons, and DataGrid
objects as seen in Figure 12.1 on the form with the properties set as in Table 12.3.
Table 12.3. Label, TextBox, and Button Control Property Settings
Object Property Setting
Label Text Last Name
TextBox ID txtLastName
Label Text First Name
TextBox ID txtFirstName
Button ID btnAdd
Text Add to DataTable
Button ID btnCreateXMLFile
Text Create XML File
DataGrid ID dgDataToWrite
HyperLink ID hplReturnToMain
NavigateURL wfrmMain.aspx
2. Add the following line to the code module of the form. Place it under the line that
reads Web Form Designer Generated Code.
3. Dim mdtData As New DataTable()
4. Add the code in Listing 12.1 to the Load event of the page. If the data table has not
been saved to the Session object, then you need to create it from scratch by first
mdtData = CType(Session("MyDataTable"), DataTable)
End If
BindTheGrid()
End Sub
5. Create the routine BindTheGrid, shown in Listing 12.2, in the code module for the
page.
Listing 12.2 wfrmHowTo12_1.aspx.vb: Binding the Data Table to the Data
Grid
Sub BindTheGrid()
dgDataToWrite.DataSource = mdtData
dgDataToWrite.DataBind()
End Sub
6. Add the code in Listing 12.3 to the Click event of the btnAdd button. This routine
starts off by calling the NewRow method off the mdtData data table, thus creating
a new DataRow object. The two columns in drNew are replaced with the values in
txtLastName and txtFirstName. The new row is added to the data table, and the
text boxes are cleared. Last, mdtData is rebound to the data grid by calling
BindTheGrid.
Listing 12.3 wfrmHowTo12_1.aspx.vb: Adding Data to the Data Table and
Then Rebinding the Data Grid
Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd.Click
Dim drNew As DataRow
drNew = mdtData.NewRow()