AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=602&pId=-1
Reading XML News
page
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 20549/ 27

Introduction

If you run a site that displays news, or headlines or something and you want an easy way to add and edit those pages then this article is for you. This article will show you how to display news headlines on a site with a little bit of ASP.NET. Also, you should see the 'Live Demo' at the bottom of most code samples, this links to a page where Ialready have something like this set up.

Browsing XML with ASP.NET

In ASP I found navigating an XML file without actually seeing it was extremely difficult and ASP.NET didn't help much. ASP.NET has several objects that you can use to browse an XML file, after much searching, exploring and experimenting I found that the XMLTextReader was the best one for the job (and the one that didn't give me as many annoying error messages). Someone should really create a visual XML to ASP.NET creator.

The XMLTextReader has one basic function - Read XML, that's it, not write or edit or validate, read. It's much like the DataReader, but with XML. But on to the XML code.

The News.xml File

News.xml is the file that contains all of the news. It's structure is quite important (otherwise it doesn't work), however some elements can be swapped around.

  • <news>

    • <item>

      • <headline> - must be at the top.

      • <byline> - doesn't have to be, but recommended.

      • <blurb> - doesn't have to be, but recommended.

      • <link> - has to be here.

      • <image> - has to be here, has to contain an image (you can set it to a blank image).

    • </item>

  • </news>

The news.xml that we're going to be using is here.

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.

Tips for Tweaks

You can probably see that there are some additions to the code that would have made it more flexible, but I don't want to show you pages and pages of code. Some additions that wouldn't take a lot of work could include -

  • Add parameters to the Display_News() sub. Control what is displayed (image, byline, blurb etc.), control how it is displayed (font, table/flowing) etc.
  • Make the headline/image the link. This would take a bit more work, but assuming that you keep to the same XML guidelines it should be fine.
  • If multiple people are going to be using the file then you should add a schema an XML Validator to make sure that they stick to the guidelines.

Summary

You can use this code just about anywhere to parse an XML file, not just for a news page. There are some tweaks there that you may want to include to improve the code, you could also use the XMLTextWriter to build a page that adds new news and removes old news. Also see in the left column that you can discuss the article as well as some other stuff.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 8:50:21 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search