CHAPTER 12 CASE STUDY: USING .NET FOR AN XML APPLICATION 371 Figure 12-6. Handling updating errors The next subroutine, GridViewDeleted, is similar: Sub GridViewDeleted(ByVal s As Object, ByVal e As GridViewDeletedEventArgs) If Not e.Exception Is Nothing Then lblError.Text = “ Error: Could not delete row” e.ExceptionHandled = True End If End Sub Again, the page displays an error if it can t delete a row. The code block also contains a subroutine called addNews. This subroutine handles the Add News button click event: Sub addNews(sender As Object, e As System.EventArgs) response.redirect(“addNews.aspx”) end sub When users click the Add Newsbutton, the browser redirects to the addNews.aspx page. The page adds new content to the database. In case you need quality webspace to host and run your web applications, try our personal web hosting services.
370 CHAPTER 12 CASE STUDY: USING .NET FOR AN XML APPLICATION Display=”Dynamic” Text=”Please enter a title” /> <%#Eval("newsDescription")%>‘ Width=”175px”/> We recommend high quality webhost to host and run your jsp application: christian web host services.
368 CHAPTER 12 CASE STUDY: USING .NET FOR AN XML APPLICATION manageNews.aspx The manageNews.aspx page displays the content from the database in a GridView control, as shown in Figure 12-5. Notice that the control renders as a table. Figure 12-5. The manageNews.aspx page The news items display in a table. Each column represents a field from the news table. The table headings are links that you can click to sort the columns. This page also includes automatically generated Edit and Delete links. Users need to click the Add News button to add a new item. The code to create this page follows: <%@ Page Language="VB" masterpagefile="template.master" %> The code creates the following structure:
http://www.apress.com The latest XML browser news.The .NET Framework includes five namespaces to implement the XML core standards. I ve only included one element in the document for brevity. The only control on the page is an AccessDataSource control: The control has the id of NewsDS, and I refer to this when creating a DataView object, as you saw earlier. The code sets the DataSourceMode to DataSet so the page can access the content programmatically in a DataView object. It also specifies the DataFile property and SelectCommand. The remainder of the application deals with managing the news content within the database. From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.
366 CHAPTER 12 CASE STUDY: USING .NET FOR AN XML APPLICATION The next code section starts writing the XML stream, using the WriteStartDocument() method to generate the XML declaration. The WriteStartElement() method creates the root element, and sets the version attribute of this element to 2.0: XMLFeed.WriteStartDocument() XMLFeed.WriteStartElement(“rss”) XMLFeed.WriteAttributeString(“version”, “2.0″) Next, the code creates the element along with the
http://www.apress.com The latest XML browser news. The code generates the remaining elements by looping through the content in the DataView. It accesses each data row in the rows collection. Each time the code finds a news item, it writes a starting element and adds the and elements. The WriteElementString() method creates the opening tag, adds the specified text, and generates the closing tag: For Each dr As DataRow In dv.Table.Rows XMLFeed.WriteStartElement(“item”) XMLFeed.WriteElementString(“title”, dr(“newsTitle”).ToString()) XMLFeed.WriteElementString(“description”, dr(“newsDescription”).ToString()) XMLFeed.WriteEndElement() Next The script block finishes by writing the closing elements for each of the elements created earlier. It also calls the Flush() method to flush whatever is in the buffer to the stream, and it uses the Close() method to close the stream: We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.
CHAPTER 12 CASE STUDY: USING .NET FOR AN XML APPLICATION 365 XMLFeed.WriteStartElement(“item”) XMLFeed.WriteElementString(“title”, dr(“newsTitle”).ToString()) XMLFeed.WriteElementString(“description”, dr(“newsDescription”).ToString()) XMLFeed.WriteEndElement() Next XMLFeed.WriteEndElement() XMLFeed.WriteEndElement() XMLFeed.WriteEndDocument() XMLFeed.Flush() XMLFeed.Close() Response.End() End sub This page doesn t use the master page, as it contains only XML content. The page starts by declaring the language and importing namespaces: <%@ Page Language="VB" %> <%@ import Namespace="System.IO" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Xml" %> It then runs code in response to the page loadevent. The Page_Load subroutine starts by declaring the content type as text/xml: Sub Page_Load(Src As Object, E As EventArgs) Response.ContentType = “text/xml” The code then declares a DataView object that takes its content from the NewsDSAccessDataSource control: Dim dv As DataView = CType(NewsDS.Select(DataSourceSelectArguments.Empty), . DataView) The DataView allows the page to access the contents of the AccessDataSourcecontrol programmatically. The code uses the contents to generate the RSS feed. It starts by creating a new XmlTextWriter object: Dim XMLFeed as XmlTextWriter = new XmlTextWriter(Response.OutputStream, . Encoding.UTF8) The code sets the stream to Response.OutputStream and the encoding to UTF8. It could also specify a physical file for the XML stream. We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.
364 CHAPTER 12 CASE STUDY: USING .NET FOR AN XML APPLICATION In this case, the code uses an XmlTextWriter object to create the XML stream and generate the content for the news feed. I didn t cover this object in Chapter 11, so Table 12-2 provides a summary of the most important methods. Table 12-2. The Most Important Methods of the XmlTextWriter Class Method Explanation WriteStartDocument() Writes the XML declaration, using version 1.0 WriteEndDocument() Closes open elements or attributes WriteComment() Writes a comment WriteProcessingInstruction() Writes a processing instruction WriteDocType() Writes the DOCTYPE declaration WriteStartElement() Writes a starting tag WriteEndElement() Closes the current tag WriteElementString() Writes an element including text WriteStartAttribute() Writes the start of an attribute WriteEndAttribute() Writes the end of an attribute WriteAttributes() Writes an attribute Flush() Flushes the buffer to the stream Close() Closes the XML stream You ll see many of these methods used in the rss.aspx page, which follows: <%@ Page Language="VB" %> <%@ import Namespace="System.IO" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Xml" %>