Answers for Day 11

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Appendix A.  Answers to Quiz Questions


Quiz

1:

When should you use an XmlTextReader versus an XmlDocument?

A1:

An XmlTextReader should be used when you only need to view data and don't need any higher-level functionality. The XmlDocument provides more functionality using the XML Document Object Model, but it introduces additional overhead.

2:

What does XML DOM stand for?

A1:

Extensible Markup Language Document Object Model.

3:

What's the difference between a node and an element?

A1:

An element is represented by a single tag in an XML file. A node is an entire branch of data, consisting of start and end tags, children elements, and any attributes.

4:

Will the following code snippet work?

 dim xmldoc as new XMLDataDocument() xmldoc.DataSet.ReadXml("books.xml") 
A1:

No, the ReadXml requires the full path to the XML data file. The last line should read as follows:

 xmldoc.DataSet.ReadXml(Server.MapPath("books.xml")) 

Exercises

1:

Create a new XML file that uses the books.xdr schema. Make sure your format is correct by using ASP.NET to validate it.

2:

Build an interface that allows users to edit XML data from a DataGrid. Be sure to write the changes back to the file.

A1:

Here's the code for the solution:

 1:    <%@ Page Language="VB" %> 2:    <%@ Import Namespace="System.Xml" %> 3:    <%@ Import Namespace="System.Data" %> 4:    <%@ Import Namespace="System.Data.OleDb" %> 5: 6:    <script runat=server> 7:       private i, j as integer 8:       private strOutput as string = "" 9:       public xmldoc as new XMLDataDocument() 10: 11:       sub Page_Load(Sender as Object, e as EventArgs) 12:          if not Page.IsPostBack then 13:             GetData() 14:             BindControl() 15:          end if 16:       end sub 17: 18:       sub UpdateBtn_Click(Sender as Object, e as EventArgs) 19:          dim Title as TextBox 20:          dim Genre as TextBox 21:          dim Style as TextBox 22:          dim Price as TextBox 23: 24:          GetData() 25: 26:          'update data 27:          For i = 0 To DataGrid1.Items.Count-1 28:             Title = DataGrid1.Items(i).FindControl("Title") 29:             Genre = DataGrid1.Items(i).FindControl("Genre") 30:             Style = DataGrid1.Items(i).FindControl("Style") 31:             Price = DataGrid1.Items(i).FindControl("Price") 32: 33:             xmldoc.DataSet.Tables(0).Rows(i)("title") = _ 34:                Title.Text 35:             xmldoc.DataSet.Tables(0).Rows(i)("genre") = _ 36:                Genre.Text 37:             xmldoc.DataSet.Tables(0).Rows(i)("style") = _ 38:                Style.Text 39:             xmldoc.DataSet.Tables(0).Rows(i)("price") = _ 40:                Price.Text 41:          Next 42: 43:          try 44:             xmldoc.Save(Server.MapPath("books.xml")) 45:          catch 46:             output.Text = "Error updating data" 47:          end try 48: 49:          BindControl() 50:       end sub 51: 52:       sub GetData() 53:          try 54:             xmldoc.DataSet.ReadXml(Server.MapPath("books.xml")) 55:          catch ex as Exception 56:             output.Text = "Error accessing XML file" 57:          end try 58:       end sub 59: 60:       sub BindControl() 61:          DataGrid1.DataSource = xmldoc.DataSet 62:          DataGrid1.DataMember = xmldoc.DataSet.Tables(0). _ 63:             TableName 64:          DataGrid1.DataBind() 65:       end sub 66:    </script> 67: 68:    <html><body> 69:       <asp:Label  runat="server" /> 70: 71:       <form runat="server"> 72: 73:       <asp:DataGrid  74:          BorderColor="black" 75:          GridLines="Vertical" 76:          width="450" 77:          Font-Name="Arial" 78:          Font-Size="10pt" 79:          HeaderStyle-BackColor="#cccc99" 80:          ItemStyle-BackColor="#ffffff" 81:          AlternatingItemStyle-Backcolor="#cccccc" 82:          AutogenerateColumns="false" runat="server" > 83: 84:          <Columns> 85: 86:             <asp:TemplateColumn HeaderText="Title"> 87:                <ItemTemplate> 88:                   <asp:TextBox  runat="server" 89:                      Text='<%# Container.DataItem("title") %>' 90:                    /> 91:                </ItemTemplate> 92:             </asp:TemplateColumn> 93: 94:             <asp:TemplateColumn HeaderText="Genre"> 95:                <ItemTemplate> 96:                   <asp:TextBox  runat="server" 97:                      Text='<%# Container.DataItem("genre") %>' 98:                      width="75px" /> 99:                </ItemTemplate> 100:             </asp:TemplateColumn> 101: 102:             <asp:TemplateColumn HeaderText="style"> 103:                <ItemTemplate> 104:                   <asp:TextBox  runat="server" 105:                      Text='<%# Container.DataItem("style") %>' 106:                      width="75px" /> 107:                </ItemTemplate> 108:             </asp:TemplateColumn> 109: 110:             <asp:TemplateColumn HeaderText="Price"> 111:                <ItemTemplate> 112:                   <asp:TextBox  runat="server" 113:                      Text='<%# Container.DataItem("price") %>' 114:                      width="50px" /> 115:                </ItemTemplate> 116:             </asp:TemplateColumn> 117: 118:          </Columns> 119:       </asp:DataGrid> 120:       <p> 121:       <center> 122:       <asp:Button  runat="server" 123:          OnClick="UpdateBtn_Click" 124:          text="Update!" /> 125:       </center> 126: 127:       </form> 128: 129:    </body></html> 


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon

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