CSTrüter HomeArticlesDownloadsAbout meContact me
Modify WebControl markup via WebControlAdapter 2010-03-11 21:47:12
a quick look at how to create a windows service using C# 2010-02-28 21:48:06
How to call server-side code from client-side code, using PageMethods in ASP.net 2010-02-21 12:31:27
How to pass a set of data to an xml type in SQL 2005/8 2010-02-12 19:04:23
Some funky behaviour regarding overload Resolution of dynamic/object types 2010-02-09 17:16:52
Object orientated programming within JavaScript 2010-01-28 07:25:45
How to sort data using ASP.net (C#) and SQL 2005/8 2010-01-18 15:23:14
Quick look at some of the new features added to C# 4.0 2010-01-12 21:52:13
SQL 2008 introduced a nifty feature called Table-Valued Parameters (TVP) into its codebase 2010-01-06 22:58:25
How to page data using ASP.net (C#) and SQL 2005/8 2009-10-19 15:01:45
Creating a WYSIWYG textbox for your website is actually quite simple. 2007-02-01 12:00:00
Move items between two listboxes in ASP.net(C#, VB.NET) and PHP 2008-06-12 17:07:43
Firefox word wrapping issues 2008-06-09 09:51:21
2007-02-22 12:00:00
Blog about passing parameters by reference to functions using func_get_arg(s) 2008-07-27 12:38:24
Download the demo code There is a number of ways to read a RSS feed in .net, I am going to look at four different solutions to do this. The following code is by no means a mature solution (it assumes a lot of things), and should soley be used as a starting point - ensure that you've got sufficient exception handling. Looking at the namespaces, we'll be using a generic list (System.Collections.Generic) for our items, which we can easily bind to a datasource. We're going to have a look at using Linq (System.Linq & System.Xml.Linq), and have a look at Microsofts' WCF syndication (System.ServiceModel.Syndication - which you can find in the System.ServiceModel.Web component in .net 3.5)
using System.Collections.Generic; using System.Linq; using System.ServiceModel.Syndication; using System.Xml; using System.Xml.Linq;
Imports System.Collections.Generic Imports System.Linq imports System.ServiceModel.Syndication imports System.Xml imports System.Xml.Linq
public struct RSSItem { private string _title; public string title { get { return _title; } set { _title = value; } } private string _link; public string link { get { return _link; } set { _link = value; } } private string _description; public string description { get { return _description; } set { _description = value; } } }
Public Structure RSSItem Private _title As String Public Property title() As String Get Return _title End Get Set(ByVal value As String) _title = value End Set End Property Private _link As String Public Property link() As String Get Return _link End Get Set(ByVal value As String) _link = value End Set End Property Private _description As String Public Property description() As String Get Return _description End Get Set(ByVal value As String) _description = value End Set End Property End Structure
public class RSSReader { public string title; public string link; public string description; public List<RSSItem> items = new List<RSSItem>(); public RSSReader(string url) { XmlDocument doc = new XmlDocument(); doc.Load(url); XmlElement channel = doc["rss"]["channel"]; XmlNodeList items = channel.GetElementsByTagName("item"); this.title = channel["title"].InnerText; this.link = channel["link"].InnerText; this.description = channel["description"].InnerText; foreach (XmlNode item in items) { RSSItem rssItem = new RSSItem(); rssItem.title = item["title"].InnerText; rssItem.description = item["description"].InnerText; rssItem.link = item["link"].InnerText; this.items.Add(rssItem); } } }
Public Class RSSReader Public title As String Public link As String Public description As String Public items As List(Of RSSItem) = New List(Of RSSItem)() Public Sub New(ByVal url As String) Dim doc As New XmlDocument() doc.Load(url) Dim channel As XmlElement = doc("rss")("channel") Dim items As XmlNodeList = channel.GetElementsByTagName("item") Me.title = channel("title").InnerText Me.link = channel("link").InnerText Me.description = channel("description").InnerText For Each item As XmlNode In items Dim rss As New RSSItem() rss.title = item("title").InnerText rss.description = item("description").InnerText rss.link = item("link").InnerText Me.items.Add(rss) Next End Sub End Class
public class RSSReader { public string title; public string link; public string description; public List<RSSItem> items; public RSSReader(string url) { XElement rssFeed = XElement.Load(url).Element("channel"); this.title = rssFeed.Element("title").Value; this.link = rssFeed.Element("link").Value; this.description = rssFeed.Element("description").Value; items = (from Item in rssFeed.Elements("item") select new RSSItem() { title = Item.Element("title").Value, description = Item.Element("description").Value, link = Item.Element("link").Value }).ToList<RSSItem>(); } }
Public Class RSSReader Public title As String Public link As String Public description As String Public items As List(Of RSSItem) Public Sub New(ByVal url As String) Dim rssFeed As XElement = XElement.Load(url).Element("channel") Me.title = rssFeed.Element("title").Value Me.link = rssFeed.Element("link").Value Me.description = rssFeed.Element("description").Value items = (From Item In rssFeed.Elements("item") _ Select New RSSItem With { _ .title = Item.Element("title").Value, _ .description = Item.Element("description").Value, _ .link = Item.Element("link").Value}).ToList() End Sub End Class
public class RSSReader { public string title; public string link; public string description; public List<RSSItem> items = new List<RSSItem>(); public RSSReader(string url) { XmlTextReader reader = new XmlTextReader(url); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case "title": this.title = reader.ReadString(); break; case "link": this.link = reader.ReadString(); break; case "description": this.description = reader.ReadString(); break; case "image": while (!((reader.Name == "image") && (reader.NodeType == XmlNodeType.EndElement)) && reader.Read()) break; } } if (reader.Name == "item") { RSSItem item = new RSSItem(); while (!((reader.Name == "item") && (reader.NodeType == XmlNodeType.EndElement)) && reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name.ToLower()) { case "title": item.title = reader.ReadString(); break; case "description": item.description = reader.ReadString(); break; case "link": item.link = reader.ReadString(); break; } } } items.Add(item); } } } }
Public Class RSSReader Public title As String Public link As String Public description As String Public items As List(Of RSSItem) = New List(Of RSSItem)() Public Sub New(ByVal url As String) Dim reader As New XmlTextReader(url) While reader.Read() If reader.NodeType = XmlNodeType.Element Then Select Case (reader.Name) Case "title" Me.title = reader.ReadString() Case "link" Me.link = reader.ReadString() Case "description" Me.description = reader.ReadString() Case "image" While (Not ((reader.Name = "image") And _ (reader.NodeType = XmlNodeType.EndElement)) And _ reader.Read()) End While End Select End If If reader.Name = "item" Then Dim item As New RSSItem() While (Not ((reader.Name = "item") And _ (reader.NodeType = XmlNodeType.EndElement)) And _ reader.Read()) If (reader.NodeType = XmlNodeType.Element) Then Select Case (reader.Name.ToLower()) Case "title" item.title = reader.ReadString() Case "description" item.description = reader.ReadString() Case "link" item.link = reader.ReadString() End Select End If End While items.Add(item) End If End While End Sub
XmlReader RSSReader = XmlReader.Create(@"http://rss.cnn.com/rss/cnn_space.rss"); Rss20FeedFormatter formatter = new Rss20FeedFormatter(); formatter.ReadFrom(RSSReader);
Dim RSSReader As XmlReader = XmlReader.Create("http://rss.cnn.com/rss/cnn_space.rss") Dim formatter As Rss20FeedFormatter = New Rss20FeedFormatter() formatter.ReadFrom(RSSReader)
Hi Buck In you're iteration you're adding the title of the main object - eg myItems.title, simply change it to rss.title (since thats the instance of each iterated collection) eg Dim myItems As New rsi.RSSReader(txtURL.Text) For Each rss In myItems.items ListBox1.Items.Add(rss.title) Next
Thank you for this code sample. It is exactly what I was looking for. I am having an issue with that I am hoping you can help me with. I am trying to add the individual RSS posts into a listbox. This adds the title of the forum for each post put I want to add the title of the post into it. What am I missing? Dim myItems As New rsi.RSSReader(txtURL.Text) For Each rss In myItems.items ListBox1.Items.Add(myItems.title) Next
Codebooth my semi-community site etc (work in progress)
The company I'am currently working for as software developer.
a Parallel reference of programming languages 2009-09-10 12:48:23
a tutorial explaining how to develop a simple login using PHP and MySQL 2009-09-05 18:26:47
An article looking at adding some kind of event driven model to PHP 5 2008-07-28 12:48:09
It is very simple creating your own rss reader, the following article looks at a few methods of doing this. 2008-06-23 13:18:25
A quick reference about working with dropdown boxes (select element) in javascript. 2007-02-17 16:36:41
Collection of funny programming articles 2006-10-08 14:23:43