Reading and Writing XML Files with the DataSet

The Xml server control is not your only option for rendering XML data in a browser. You also can read XML documents into a DataSet and bind the DataSet to any available server control, such as the DataGrid. Using the System.IO.StreamReader class, you can stream in XML data and use the DataSet.ReadXml() method to dynamically create a DataTable based on the XML data. Listing 12.12 shows how to do this in a Web Form.

Listing 12.12. Reading XML Data into a DataSet and Binding It to a DataGrid
[VB]

01: <%@ Import Namespace="System.Data" %>
02: <%@ Import Namespace="System.Xml" %>
03: <script language="VB" runat=server>
04: Protected Sub Page_Load(Sender As Object, E As EventArgs)
05:  Dim myDataSet As New DataSet
06:  Dim myStreamReader As System.IO.StreamReader = New System.IO.StreamReader(Server
.MapPath("users.xml"))
07:  myDataSet.ReadXml( myStreamReader )
08:  myStreamReader.Close()
09:  myDataGrid.DataSource = myDataSet.Tables(0).DefaultView
10:  Page.DataBind()
11: End Sub

12: </script>

[C#]
01: <%@ Import Namespace="System.Data" %>
02: <%@ Import Namespace="System.Xml" %>
03: <script language="C#" runat=server>
04: protected void Page_Load(Object sender, EventArgs e){
05:  DataSet myDataSet = new DataSet();
06:  System.IO.StreamReader myStreamReader = new System.IO.StreamReader(Server.MapPath
("users.xml"));
07:  myDataSet.ReadXml( myStreamReader );
08:  myStreamReader.Close();
09:  myDataGrid.DataSource = myDataSet.Tables[0].DefaultView;
10:  Page.DataBind();
11: }
12: </script>

[VB & C#]

13: <html>
14: <head>
15: <title>Programming Datadriven Web Apps - Chapter 12</title>
16: <style rel="stylesheet" type="text/css">
17:  H3 {  font: bold 11pt Verdana, Arial, sans-serif;}
18:  .label {  font: bold 9pt Verdana, Arial, sans-serif;}
19:  .header {  font: bold 9pt Verdana, Arial, sans-serif; background-color:tan; color:
 black;}
20:  .value {  font: 8pt Verdana, Arial, sans-serif;}
21: </style>
22: </head>
23: <body>
24: <form runat="server">
25: <H3>Current Users</H3>
26: <asp:DataGrid id="myDataGrid" runat="server"
27: BorderWidth="0" Cellpadding="2" Cellspacing="0"
28: HeaderStyle-CssClass="header" ItemStyle-CssClass="value" />
29: </form>
30: </body>
31: </html>

In Listing 12.12, you create a Web Form that streams in XML data from the users.xml file. On line 6, you create a System.IO.StreamReader using the path to the users.xml file. On line 7, you use the DataSet.ReadXml() method to read the StreamReader into the DataSet. This creates a new DataTable representing the users.xml file. You then simply bind the DataTable to a DataGrid. Figure 12.7 shows the rendered output from Listing 12.12.

Figure 12.7. You can read XML data into a DataSet using a StreamReader and the DataSet.ReadXml() method.


As much as you read and wrote XML data using the XmlDocument and XmlElement classes in the previous listings, you can use the DataSet to read and write XML data. In Listing 12.12, you used the StreamReader class to stream in XML data, and the DataSet.ReadXml() method to create a DataTable from the XML data. In Listing 12.13 you extend this to have a form identical to that in Listing 12.11. In the Submit_Click() event handler, you create a new DataRow in the DataTable, and then use the DataSet.WriteXml() method to write the DataTable to the file system as an XML document.

Listing 12.13. Reading and Writing XML Data with the DataSet
[VB]

01: <%@ Import Namespace="System.Data" %>
02: <%@ Import Namespace="System.Xml" %>
03: <script language="VB" runat=server>
04: Protected Sub Page_Load(Sender As Object, E As EventArgs)
05:  Dim myDataSet As New DataSet
06:  Dim myStreamReader As System.IO.StreamReader = New System.IO.StreamReader(Server
.MapPath("users.xml"))
07:  myDataSet.ReadXml( myStreamReader )
08:  myStreamReader.Close()
09:
10:  If IsPostBack Then
11:  Dim myNewRow As DataRow = myDataSet.Tables(0).NewRow()
12:  myNewRow("Name") = Name.Text
13:  myNewRow("Email") = Email.Text
14:  myNewRow("Password") = Password.Text
15:  myDataSet.Tables(0).Rows.Add(myNewRow)
16:  myDataSet.AcceptChanges()
17:  myDataSet.WriteXml(Server.MapPath("users.xml"), XmlWriteMode.IgnoreSchema)
18:  End If
19:
20:  myDataGrid.DataSource = myDataSet.Tables(0).DefaultView
21:  Page.DataBind()
22: End Sub
23: </script>

[C#]

01: <%@ Import Namespace="System.Data" %>
02: <%@ Import Namespace="System.Xml" %>
03: <script language="C#" runat=server>
04: protected void Page_Load(Object sender, EventArgs e){
05:  DataSet myDataSet = new DataSet();
06:  System.IO.StreamReader myStreamReader = new System.IO.StreamReader(Server.MapPath
("users.xml"));
07:  myDataSet.ReadXml( myStreamReader );
08:  myStreamReader.Close();
09:
10:  if(IsPostBack){
11:  DataRow myNewRow = myDataSet.Tables[0].NewRow();
12:  myNewRow["Name"] = Name.Text;
13:  myNewRow["Email"] = Email.Text;
14:  myNewRow["Password"] = Password.Text;
15:  myDataSet.Tables[0].Rows.Add(myNewRow);
16:  myDataSet.AcceptChanges();
17:  myDataSet.WriteXml(Server.MapPath("users.xml"), XmlWriteMode.IgnoreSchema);
18:  }
19:
20:  myDataGrid.DataSource = myDataSet.Tables[0].DefaultView;
21:  Page.DataBind();
22: }
23: </script>

[VB & C#]

24: <html>
25: <head>
26: <title>Programming Datadriven Web Apps - Chapter 12</title>
27: <style rel="stylesheet" type="text/css">
28:  H3 {  font: bold 11pt Verdana, Arial, sans-serif;}
29:  .label {  font: bold 9pt Verdana, Arial, sans-serif;}
30:  .header {  font: bold 9pt Verdana, Arial, sans-serif; background-color:tan; color:
 black;}
31:  .value {  font: 8pt Verdana, Arial, sans-serif;}
32: </style>
33: </head>
34: <body>
35: <form runat="server">
36: <H3>Add a New User</H3>
37: <table border="0" cellpadding="2" cellspacing="0">
38: <tr>
39:  <td class="label">Name:</td>
40:  <td>
41:  <asp:TextBox runat="server" id="Name" Width="200"
42:   BorderStyle="Solid" BorderWidth="1"
43:   Font-Size="8pt" Font-Name="Verdana" />
44:  </td>
45: </tr>
46: <tr>
47:  <td class="label">E-Mail:</td>
48:  <td>
49:  <asp:TextBox runat="server" id="Email" Width="200"
50:   BorderStyle="Solid" BorderWidth="1"
51:   Font-Size="8pt" Font-Name="Verdana" />
52:  </td>
53: </tr>
54: <tr>
55:  <td class="label">Password:</td>
56:  <td>
57:  <asp:TextBox runat="server" id="Password" Width="200"
58:   BorderStyle="Solid" BorderWidth="1"
59:   Font-Size="8pt" Font-Name="Verdana" />
60:  </td>
61: </tr>
62: <tr>
63:  <td></td>
64:  <td>
65:  <asp:Button runat="server" id="Submit" Text="Submit New User"
66:   Width="200" BorderStyle="Solid" BorderWidth="1"
67:   Font-Size="8pt" Font-Name="Verdana" />
68:  </td>
69: </tr>
70: </table>
71: <H3>Current Users</H3>
72: <asp:DataGrid id="myDataGrid" runat="server"
73: BorderWidth="0" Cellpadding="2" Cellspacing="0"
74: HeaderStyle-CssClass="header" ItemStyle-CssClass="value" />
75: </form>
76: </body>
77: </html>

In Listing 12.13, you create a Web Form like that in Listing 12.11. The Web Form has TextBoxes for Name, Email, and Password. When the Submit New User button is clicked, the Page.IsPostBack property is set to True, and the If IsPostBack Then... code on line 10 is executed.

On line 11, you create a new DataRow from the DataTable that holds the XML data. On lines 12 through 14, you set the values of the three columns in the DataRow, Name, Email, and Password. On line 15, you add the DataRow to the DataTable by calling DataTable.Rows.Add(DataRow). On line 16, you call DataTable.AcceptChanges() to commit the new DataRow to the DataTable (effectively saying, “Yes, I want this row”). On line 17, you call DataSet.WriteXml() and pass in the path to write the XML as the first parameter, and XmlWriteMode.IgnoreSchema as the second parameter. The XmlWriteMode.IgnoreSchema enumerator tells the .NET Framework not to write the XML schema information to the file. You do this because the XML document did not have the schema information to begin with. If the schema were part of the original XML file, you could write the schema by using the XmlWriteMode.IgnoreSchema enumerator (the default value). Figure 12.8 shows the rendered output from Listing 12.13 after adding a new user. Table 12.2 shows the XmlWriteMode enumerators.

Figure 12.8. You can use the DataSet.WriteXml() method to write a DataTable to the file system as an XML document.


Table 12.2. XmlWriteMode Enumerators
Value Description
DiffGram Writes the entire DataSet as a DiffGram, including current and original values.
IgnoreSchema Writes the current contents of the DataSet as XML data without an XSD schema. The XSD can be written separately using DataSet.WriteXmlSchema().
WriteSchema Writes the current contents of the DataSet as XML data with the relational structure as inline XSD schema. This is the default value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.149.180