XML News Feeds

Thanks for your interest in the XML News Feeds. This application provides a compilation of news headlines from many sources, using the MoreOver.com's XML service. It is built on the .NET framework and uses C# as the ASP.NET server side language.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 18
September 04, 2002
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

The pages we built using Visual Studio .NET.

Requirements:

These files require a Web Server with the Microsoft .NET framework installed to function. To retrieve the XML news feeds, a connection to the internet from the application is necessary.

Installation Instructions:

Unzip the files and import them in a web, or create a new web with these files. Build the application from Visual Studio or compile using the .NET compilier. They should work as is. You can customize the look with the Style tags or simply copy past the code into your own web forms. Browse to the news.aspx page to start the application.

Use Cases:

News Browser: Comes to the News.aspx page and is presented with the Top Stories headlines. Using the left navigation, they can browse many topics divided into several Sections. System: The system will make a call to MoreOver.com's web site to get the XML news feed. Before each call, a check will be made to see if the headlines have been Cached. Caching occurs for each topic, and expires after a set time period, 10 minutes default.

Adding Categories:

To add your own categories, you will need to seek out the category name from MoreOver.com's web site, then add them to the Drop-Down list values. They can be found here: http://w.moreover.com/categories/category_list.html.

File Descriptions:

News.aspx: Displays the news and Navigation News.aspx.cs: Holds the Server-side processing and events fired from the Navigation drop-down boxes.

MoreOverNews.cs: Holds the functionality to make the MoreOver.com requests and format the headlines.

Contact:

Please report and bugs, and we appreciate your comments or suggestions. We can be reached at webmaster@HarrisonLogic.com or visit us at www.HarrisonLogic.com.

MoreOverNews.cs


using System
using System.IO
using System.Text
using System.Net
using System.Xml.XPath
namespace HL.xmlNewsFeeds 
///<summary />
/// MoreOverNews.cs: This class holds methods used to retrieve 
/// MoreOver XML 
/// news feeds from their site. You will have to know the 
/// Catagory to use in 
/// the MoreOver uri. We have compiled several for you to use,
/// however you can 
/// find others by visiting their site. - HarrisonLogic 
///</summary />
public class MoreOverNews 
public static 
string GetNews(string Category
int NoReturnedArticles
string articlePrefixstring articleSuffix) { 
/* PARAMETERS: Category: MoreOver.com's Topic category, 
   find legal Categories from their site 
   NoReturnedArticles: Number of article links you would 
   like retrieved 
   articlePrefix: Code inserted in from of each article link. 
   (i.e. </p>, <li>,<td />..etc) 
   articleSuffix: Code inserted at the end of each article link. */ 
if (NoReturnedArticles 25 NoReturnedArticles <1) { 
// Default to 5 Headlines if not within range 
NoReturnedArticles 5; } 
// Use this news Uri from MoreOver.com 
string newsUri "http://p.moreover.com/cgi-local/page?c=" 
Category.Trim() + "&o=xml"
// Use the String builder object, more efficient than a string 
StringBuilder newsContent = new StringBuilder(1024); 
// Keep track of number of Articles 
int articleCount 0
// Use the WebRequest / Response objects, allows us to set a timeout 
WebRequest moReq
WebResponse moRes
Stream moStream
// Set timeout for five seconds 
moReq WebRequest.Create(newsUri); 
moReq.Timeout 5000
// Get the Response and insert in the Stream object 
moRes moReq.GetResponse(); 
moStream moRes.GetResponseStream(); 
//Open a XPathDocument with Stream Object 
XPathDocument doc=new XPathDocument(moStream); 
//Create the XPath navigator 
XPathNavigator nav=((IXPathNavigable)doc).CreateNavigator(); 
//create the XPathNodeIterator of Article nodes 
XPathNodeIterator iter=nav.Select("/moreovernews/article"); 
while(
iter.MoveNext() & articleCount != NoReturnedArticles) { 
newsContent.Append(articlePrefix LoadArticle(iter.Current
articleSuffix); articleCount += 1; } 
// Do a simple check to see if there are no articles returned, 
//this could be 
// the result of a bad Cateogry name 
if (articleCount == 0) { 
newsContent.Append("There were no articles returned for your category.


If you feel there should be articles returned, contact the system 
administrator."
); } 
return 
newsContent.ToString(); } 
private static 
string LoadArticle(XPathNavigator lstNav) { 
string headline ""
string url ""
string source ""
// We are passsed an XPathNavigator of a particular Article node 
// we will select all of the direct descendents and return the 
//article link 
XPathNodeIterator 
 iterArticle
=lstNav.SelectDescendants(XPathNodeType.Element,false); 
while(
iterArticle.MoveNext()) { 
switch(
iterArticle.Current.Name) { 
case 
"headline_text"headline iterArticle.Current.Value
break; 
case 
"url"url iterArticle.Current.Value
break; 
case 
"source"source iterArticle.Current.Value; break; } 

//return article link; 
return "<a class="newslink" href="" target="_blank">" headline 
"</a>" " <i>" source "</i>"


}

News.aspx

<!--aspPage language="c#" Codebehind="News.aspx.cs" 
AutoEventWireup="false" Inherits="HL.xmlNewsFeeds.News" -->
<
meta content="C#" name="CODE_LANGUAGE" />
<
meta content="JavaScript" name="vs_defaultClientScript" />
<
meta content="http://schemas.microsoft.com/intellisense/ie5" 
name="vs_targetSchema" />
<
style>.headingCell {BACKGROUND-COLOR#006699; color: white; 
font-stylesmall-caps }
            .
navigationCell {BACKGROUND-COLOR#FFFFCC;}
            
.newsDropDown {FONT-SIZE8ptWIDTH100px
            
FONT-FAMILYtahomaBACKGROUND-COLOR#eeeeee; }
            
td font-size10ptfont-family"Trebuchet MS"
            }</
style>
<
form id="News" method="post" runat="server">
<
table cellspacing="0" cellpadding="5" width="98%" border="0">
<
tbody>
<
tr>
<
td class="navigationCell" valign="top">
<
p>
<
table cellspacing="0" cellpadding="2" width="150" border="0">
<
tbody>
<
tr>
<
td class="headingCell">   Browse News</td></tr>
</
tr />
<
tr>
<
td>  Top Headlines

<dropdownlist id="TopHeadlines" runat="server" autopostback="True" 
cssclass="newsDropDown" onselectedindexchanged="LoadTopNews" />
<
listitem /> 
</
listitem />
<
listitem value="Top%20business%20stories" />Business
<listitem value="Top%20stories" />Top Stories
</listitem />
<
listitem value="Top%20internet%20stories" />Internet
</listitem />
<
listitem value="Top%20US%20stories" />US news
</listitem />
<
listitem value="Sports%3A%20top%20stories" />Sports
</listitem />
<
listitem value="International%20relations%20news" />International
</listitem />
<
p>Technology

<dropdownlist id="Technology" runat="server" autopostback="True" 
cssclass="newsDropDown" onselectedindexchanged="LoadTechNews" />
<
listitem /> 
</
listitem />
<
listitem value="Developer%20news" />Web Developer
<listitem value="Webmaster%20tips" />Web Master
</listitem />
<
listitem value="Java%20news" />Java
</listitem />
<
listitem value="Wireless%20sector%20news" />Wireless
</listitem /></p>
<
p> </p>
<
p>Financial

<dropdownlist id="Finance" runat="server" autopostback="True" 
cssclass="newsDropDown" onselectedindexchanged="LoadFinanceNews" />
<
listitem /> 
</
listitem />
<
listitem value="Personal%20finance%20news" />Personal Finance
<listitem value="Mutual%20funds%20news" />Mutual Funds
</listitem />
<
listitem value="Stock%20Exchanges%20news" />Stock Exchange
</listitem /></p>
<
p> </p>
<
p>Sports

<dropdownlist id="Sports" runat="server" autopostback="True" 
cssclass="newsDropDown" onselectedindexchanged="LoadSportsNews" />
<
listitem /> 
</
listitem />
<
listitem value="Sports%3A%20baseball%20news" />Baseball
<listitem value="Sports%3A%20basketball%20news" />Basketball
</listitem />
<
listitem value="Sports%3A%20boxing%20news" />Boxing
</listitem />
<
listitem value="Sports%3A%20American%20football%20news" />Football
</listitem /></p>
<
p> </p>
<
p>Special Interest

<dropdownlist id="Interest" runat="server" autopostback="True" 
cssclass="newsDropDown" onselectedindexchanged="LoadInterestNews" />
<
listitem /> 
</
listitem />
<
listitem value="Consumer%3A%20fitness%20news" />Fitness
<listitem value="Religion%20news" />Religion
</listitem />
<
listitem value="Science%20news" />Science
</listitem />
<
listitem value="Space%20science%20news" />Space
</listitem />
<
listitem value="Consumer%3A%20parenting%20news" />Parenting
</listitem /></p>
<
p> </p></td>
<
td valign="top" width="100%"><b><label id="Heading" runat="server">
 </
label></b>
<
p> </p><label id="headlines" runat="server"> </label></td>

News.aspx.cs

using System
using System.Collections
using System.ComponentModel
using System.Data
using System.Drawing
using System.Web
using System.Web.SessionState
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.HtmlControls
namespace HL.xmlNewsFeeds 
///<summary />
/// This page uses the Classes in MoreOverNews.cs to pull news
/// feed from 
/// the site MoreOver.com, which generates XML news 
///feed comiliations. 
/// 
/// I have defaulted to Top Stories when first reaching the page,
/// and if 
/// there is a blank selection. You may change at will. 
/// 
/// More downloadable applications are available at 
///ww.HarrisonLogic.com 
/// Thanks for your interest. 
///</summary />
public class News System.Web.UI.Page 
protected 
System.Web.UI.WebControls.DropDownList TopHeadlines
protected 
System.Web.UI.WebControls.DropDownList Technology
protected 
System.Web.UI.WebControls.DropDownList Interest
protected 
System.Web.UI.WebControls.Label Heading
protected 
System.Web.UI.WebControls.DropDownList Finance
protected 
System.Web.UI.WebControls.DropDownList Sports
protected 
System.Web.UI.WebControls.Label headlines
private 
void Page_Load(object senderSystem.EventArgs e) { 
if (!
this.IsPostBack) { 
// Default to Top Stories 
Heading.Text "Top Stories"
headlines.Text GetNewsFeed("Top%20stories"); } 

private 
string GetNewsFeed(string Category) { 
string newsArticles ""
// We want to check for the cache first 
if (Cache[Category] == null) { 
try { 
// Get the news from MoreOver using the 
// Methods in MoreOverNews.cs 
string MoreOverArticles ""
MoreOverArticles 
HL.xmlNewsFeeds.MoreOverNews.GetNews(Category10
"<li>""<p>"); 
// Cache for 10 minutes, see the 'Cache' documentation 
//for more info 
//on this method 
Cache.Insert(CategoryMoreOverArticlesnull
System.Web.Caching.Cache.NoAbsoluteExpiration
TimeSpan.FromMinutes(10)); 
newsArticles MoreOverArticles; } 
catch (
Exception err) { 
// Catch any errors and report to the user 
newsArticles "<i>Error: No news headlines were retrieved.</i> </p>
<p><font size="
-1">" err.Message " </font></p>"; } 

else { 
// Use the Cached information 
newsArticles Cache[Category].ToString(); } 
return 
newsArticles; } 
public 
void LoadTopNews(object senderSystem.EventArgs e) { 
if (
TopHeadlines.SelectedItem.ToString().Trim() == "") { 
Heading.Text "Top Stories"
headlines.Text GetNewsFeed("Top%20stories"); } 
else { 
Heading.Text TopHeadlines.SelectedItem.ToString().Trim(); 
headlines.Text 
GetNewsFeed(TopHeadlines.SelectedItem.Value.ToString().Trim()
); } 

public 
void LoadFinanceNews(object senderSystem.EventArgs e) { 
if (
Finance.SelectedItem.ToString().Trim() == "") { 
Heading.Text "Top Stories"
headlines.Text GetNewsFeed("Top%20stories"); } 
else { 
Heading.Text Finance.SelectedItem.ToString().Trim(); 
headlines.Text 
GetNewsFeed(Finance.SelectedItem.Value.ToString().Trim()
); } 

public 
void LoadSportsNews(object senderSystem.EventArgs e) { 
if (
Sports.SelectedItem.ToString().Trim() == "") { 
Heading.Text "Top Stories"
headlines.Text GetNewsFeed("Top%20stories"); } 
else { 
Heading.Text Sports.SelectedItem.ToString().Trim(); 
headlines.Text 
GetNewsFeed(Sports.SelectedItem.Value.ToString().Trim()
); } 

public 
void LoadTechNews(object senderSystem.EventArgs e) { 
if (
Technology.SelectedItem.ToString().Trim() == "") { 
Heading.Text "Top Stories"
headlines.Text GetNewsFeed("Top%20stories"); } 
else { 
Heading.Text Technology.SelectedItem.ToString().Trim(); 
headlines.Text 
GetNewsFeed(Technology.SelectedItem.Value.ToString().Trim()
); } 

public 
void 
LoadInterestNews
(object senderSystem.EventArgs e) { 
if (
Interest.SelectedItem.ToString().Trim() == "") { 
Heading.Text "Top Stories"
headlines.Text GetNewsFeed("Top%20stories"); } 
else { 
Heading.Text Interest.SelectedItem.ToString().Trim(); 
headlines.Text 
GetNewsFeed(Interest.SelectedItem.Value.ToString().Trim()
); } 

#region Web Form Designer generated code override protected void 
OnInit(EventArgs e) { 
// 
// CODEGEN: This call is required 
by the ASP.NET Web Form Designer// 
InitializeComponent(); base.OnInit(e); } 
///<summary />
/// Required method for Designer support - do not modify 
/// the contents of this method with the code editor. 
///</summary />
private void InitializeComponent() { 
this.Load += new System.EventHandler(this.Page_Load); } 
#endregion } 

 

 

blog comments powered by Disqus
ASP.NET CODE ARTICLES

- How to Use the ListBox Control in ASP.NET 2.0
- How to Load XML Documents in ASP.NET 2.0
- DataGrid Code
- ASP.NET Guestbook
- User Controls and Client Side Scripting
- ASP.NET Programming with Microsoft's AS...
- ASP.NET Basics (part 3): Hard Choices
- ASP.NET Basics (part 2): Not My Type
- ASP.NET Basics (part 1): Nothing But .Net
- Directory Tree Browser
- How to get the confirmation of Yes/No from a...
- Complete example using custom errors and wri...
- Paging Certain # records per page .NET style
- General Methods of formatting and Subtractin...
- .NET LinkButton web control

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials