Reading XML News
page 3 of 4
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 20478/ 32

Parsing and Rendering the XML

In news.aspx I just displayed it to a table, you can do other things as well (like out some other content on the page). I'll explain it to you piece by piece.

<%@ Import Namespace="System.XML" %>
<script language="VB" Runat="server">
Dim StrOut

 

StrOut will contain the complete table.

Sub page_load(sender as object, e as EventArgs)
Call Display_News()
End Sub

Sub Display_News()
Dim Xread as XMLTextReader = new XMLTextReader(Server.MapPath("news.xml"))
strOut += "<table border=0 cellpadding=4 cellspacing=4 width=100%>"
try
While xread.Read()
Select Case xread.NodeType
Case XMLNodeType.Element

 

Page_load calls Display_News to show the table. You can see the XMLTextReader being declared and the table being started. xread.Read() is like the DataReader.Read(), it goes through the entire XML file, every node, every element. The Select statement will only check if it is an Element.

If xread.Name = "headline" Then
strOut += "<tr><td><font face=Arial size=2><b>" & xread.ReadString() & 
"</b></font><br>"
Else If xread.Name = "byline" Then
strOut += "<font face=Arial size=1>" & xread.ReadString() & "</font><br>"
Else If xread.Name = "blurb" Then
strOut += "<font face=Arial size=2>" & xread.ReadString() & "</font><br>"
Else If xread.Name = "link" Then
strOut += "<font face=Arial size=2><a href=" & xread.ReadString() & 
">Go There!</a></font></td>"
Else If xread.Name = "image" Then
strOut += "<td><img src=" & xread.ReadString() & " border=0></td></tr>"
End If

 

If the currently selected item is an Element it will check it's name and add the appropriate string onto strOut. You can see why some elements need to be where they are.

End Select

End While
Catch ex as Exception
Response.Write("Error Happened")
Finally
xread.close()
End try

strOut += "</table>"
lblXML.Text = strOut
End Sub


</script>
<asp:label id="lblXML" runat="server" />

Live Demo

This just finished off the Sub, error trapping and displays it on the page.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 2:23:58 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search