Danh mục

Work with Datasets and XML

Số trang: 5      Loại file: pdf      Dung lượng: 22.10 KB      Lượt xem: 4      Lượt tải: 0    
Jamona

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Làm việc với datasets và XML Đôi khi, tôi phải kéo các tài liệu XML thành datasets và ngược lại. Làm thế nào để thực hiện điều này bằng cách sử dụng. NET? Kỹ thuật. NET đã phát triển một số cách để sử dụng datasets và XML cùng
Nội dung trích xuất từ tài liệu:
Work with Datasets and XML 12.5 Work with Datasets and XMLSometimes, I have to pull XML documents into datasets and vice versa. How do Iaccomplish this using .NET?Technique.NET has developed a number of ways to utilize datasets and XML together. Thesimplest use is pushing data between the two. To do this, you have the two methodsbelonging 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 lookssimilar to the other How-Tos showing how to write XML documents. However, for thisexample, I also added another button and data grid that will show the data after readingfrom the XML document.StepsOpen 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 lastand first names, and then click the button labeled Add to DataTable. When you haveadded a few names, click the button labeled Create XML File. Using Explorer, open thefile created in C:\ called test.xml. If you click Read XML File, you will see the same databecause 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 Text Create XML File DataGrid ID dgDataToWrite Button ID btnReadFile DataGrid ID dgResultsFromXML HyperLink ID hplReturnToMain NavigateURL wfrmMain.aspx2. Add the following line to the code module of the form. Then place it under the line that reads Web Form Designer Generated Code.3. Dim mdtData As New DataTable()4. Dim mdsData As New DataSet()5. Add the code in Listing 12.11 to the Load event of the page. If the data table has not been saved to the Session object, then it is created from scratch by first creating the data columns and then adding them to the data table. The DataTable object is then saved to the Session object with the name MyDataTable. A DataSet object is also created because some of the XML methods must be used from the DataSet object, rather than at the DataTable level. If the Session objects entry already exists, it is assigned back to the module variable mdtData and mdsData. Last, the data table is bound to the DataGrid object by calling the BindTheGrid routine, which is described in the next step. Listing 12.11 wfrmHowTo12_5.aspx.vb: Creating a DataTable Object from Scratch Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Put user code to initialize the page here If (Session(MyDataTable) Is Nothing) Then Dim dcFirstName As New DataColumn() dcFirstName.ColumnName = FirstName 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 Sub6. 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() End Sub7. Add the code in Listing 12.13 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.13 wfrmHowTo12_5.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() drNew.Item(LastName) = Me.txtLastName.Text drNew.Item(FirstName) = Me.txtFirstName.Text mdtData.Rows.Add(drNew) Me.txtLastName.Text = Me.txtFirstName.Text = BindTheGrid() End Sub8. Add the code in Listing 12.14 to the event of the btnCreateXMLFile button. After loading the dataset from the Session object, the WriteXML method is invoked to 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.C ...

Tài liệu được xem nhiều: