ASP.NET Code
  Home arrow ASP.NET Code arrow XML News Feeds
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

XML News Feeds
By: Reno Adipura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 14
    2002-09-04

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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.

    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 } 

     

     


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More ASP.NET Code Articles
    More By Reno Adipura

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Application development for the OLPC laptop

    The XO laptop (of the One-Laptop-Per-Child initiative) is an inexpensive laptop project intended to help educate children around the world. The XO laptop includes many innovations, such as a novel, inexpensive, and durable hardware design and the use of GNU/Linux as the underlying operating system. The XO also includes an application environment written in Python with a human interface called Sugar, accessible to everyone (including kids). Explore the Sugar APIs and learn how to develop and debug a graphical activity in Sugar using Python.
    FREE! Go There Now!


    NEW! Download DB2 9.5 for Linux, Unix, and Windows

    Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML.
    FREE! Go There Now!


    NEW! Download IBM WebSphere Portal V6.1 beta code

    Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 2: Automate builds for a real-world Tomcat project

    Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available.
    FREE! Go There Now!


    NEW! Rational 'Talks to You' Teleconference Series

    This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today!
    FREE! Go There Now!


    NEW! Rational Build Forge Express eKit

    Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Functional Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Method Composer V7.2

    Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Performance Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads.
    FREE! Go There Now!


    NEW! Webcast: Application security testing and Web compliance

    Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    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