CSTrüter HomeArticlesDownloadsAbout meContact me
How to enable the filestream feature in SQL 2008 - Alternative way to store blobs(files) via SQL 2010-08-21 19:31:56
How to create a Singleton Pattern in C# 2010-08-10 22:52:52
How to prevent that threads access shared resources concurrently via Monitor. 2010-08-06 15:31:15
A quick review of the book PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide written by Larry Ullman 2010-08-04 21:48:58
How to prevent that threads access shared resources concurrently via Mutex. 2010-08-03 14:42:36
How to stop propagation of javascript events 2010-07-25 21:59:29
Post about how Pete the web developer fixed his sitemap 2010-07-17 15:12:02
How to setup an out of process session service 2010-07-08 17:51:46
How to display/add images from/to a SQL Database 2010-07-04 23:15:15
How to register a custom URL protocol handler 2010-06-28 20:34:01
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
Populate a TreeView control in a windows application. 2009-08-27 16:01:03
2007-02-22 12:00:00
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
The company I am currently working for as software developer.
Collection of C# snippets 2010-05-22 01:06:19
Collection of MS SQL snippets 2010-05-22 00:55:15
Collection of JavaScript snippets 2010-05-22 00:37:57
Collection of ASP.net snippets 2010-05-22 00:29:56
Collection of PHP snippets 2010-05-22 00:06:45
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