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 articlePrefix, string 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<!--asp@ Page 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-style: small-caps } .navigationCell {BACKGROUND-COLOR: #FFFFCC;} .newsDropDown {FONT-SIZE: 8pt; WIDTH: 100px; FONT-FAMILY: tahoma; BACKGROUND-COLOR: #eeeeee; } td { font-size: 10pt; font-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.csusing 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 sender, System.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(Category, 10, "<li>", "<p>"); // Cache for 10 minutes, see the 'Cache' documentation //for more info //on this method Cache.Insert(Category, MoreOverArticles, null, 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 sender, System.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 sender, System.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 sender, System.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 sender, System.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 sender, System.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 developerWorks - FREE Tools! | <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
| | | | Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
| | | | Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
| | | | You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
| | | | 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!
| | | | IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
| | | | Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. FREE! Go There Now!
| | | | 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!
| | | | You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
| | | | Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |