Tài liệu Work with Datasets and XML - Pdf 92

12.5 Work with Datasets and XML
Sometimes, I have to pull XML documents into datasets and vice versa. How do I
accomplish this using .NET?
Technique
.NET has developed a number of ways to utilize datasets and XML together. The
simplest use is pushing data between the two. To do this, you have the two methods
belonging to the DataSet object: ReadXML and WriteXML. For both of these methods,
you need to provide a filename to read or write the XML document to.
To demonstrate how to take advantage of these methods, I created a form that looks
similar to the other How-Tos showing how to write XML documents. However, for this
example, I also added another button and data grid that will show the data after reading
from the XML document.
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.5: Working with Datasets and XML.
As with How-To 12.1, when the page loads, you can enter a few names. Enter the last
and first names, and then click 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. If you click Read XML File, you will see the same data
because it was read from text.xml (see Figure 12.5).
1. Create a Web Form. Then place the Labels, TextBoxes, Buttons, and DataGrid
objects as seen in Figure 12.5 on the form with the properties set as in Table 12.9.
Table 12.9. 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

dcFirstName.Caption = "First Name"

mdtData.Columns.Add(dcFirstName)

Dim dcLastName As New DataColumn()

dcLastName.ColumnName = "LastName"
dcLastName.Caption = "Last Name"

mdtData.Columns.Add(dcLastName)
mdsData.Tables.Add(mdtData)

Session("MyDataTable") = mdtData
Session("MyDataSet") = mdsData

Else
mdtData = CType(Session("MyDataTable"), DataTable)
End If

BindTheGrid()

End Sub
6. Create the routine BindTheGrid, shown in Listing 12.12, in the code module for
the page.
Listing 12.12 wfrmHowTo12_5.aspx.vb: Binding the Data Table to the Data
Grid
Sub BindTheGrid()

dgDataToWrite.DataSource = mdtData
dgDataToWrite.DataBind()

save the data into an XML document.
Listing 12.14 wfrmHowTo12_5.aspx.vb: Creating the XML Document from
the Dataset
Private Sub btnCreateXMLFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreateXMLFile.Click

mdsData = CType(Session("MyDataset"), DataSet)
mdsData.WriteXml("c:\Test.xml")

End Sub
9. Add the code in Listing 12.14 to the Click event of the btnReadFile button. Here,
the code reads the XML document by using the ReadXML method off the
dsXMLData DataSet object and then binds it to a DataGrid object.
Listing 12.15 wfrmHowTo12_5.aspx.vb: Reading the XML Document Back
into the Dataset
Private Sub btnReadFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnReadFile.Click
Dim dsXMLData As DataSet = New DataSet()

dsXMLData.ReadXml("c:\Test.xml")

Me.dgResultsFromXML.DataSource = dsXMLData
Me.dgResultsFromXML.DataBind()

End Sub
Figure 12.5. This XML document was created using XMLTextWriter.

Comments
As you can see, for both reading and writing XML document from and to datasets,
Microsoft has given us some easy commands to accomplish the task. However, remember


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status