ASP.NET Code
  Home arrow ASP.NET Code arrow XML News Feeds
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 15
    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
     
     
    ADVERTISEMENT


    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! IBM – Taking Web 2.0 to Work

    David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve.
    FREE! Go There Now!


    NEW! Applying lean thinking to the governance of software development

    Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations.
    FREE! Go There Now!


    NEW! BlammoSplat: Build a community Web site of OpenLaszlo animations, Part 3: The community animation

    Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo.
    FREE! Go There Now!


    NEW! Did you say mainframe? e-kit

    Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise.
    FREE! Go There Now!


    NEW! Download IBM Rational Developer for System z

    Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects.
    FREE! Go There Now!


    NEW! Evaluate Rational Business Developer V7.1

    Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities.
    FREE! Go There Now!


    NEW! Hacking 101

    Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Talks to You: Grady Booch on Architecture

    Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered!
    FREE! Go There Now!


    NEW! Webcast: What is new in Viper 2 for developers?

    Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications.
    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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek