WordPress RSS Feed Consumption in ASP.NET

How can I use the WordPress blog RSS feed to display my latest blog posts on my homepage? For this, I came across the following code snippet:

Function GetRSSFeed(strURL as String) as DataTable 'Get the XML data Dim reader as XmlTextReader = New XmlTextReader(strURL) 'return a new DataSet Dim ds as DataSet = New DataSet() ds.ReadXml(reader) Return ds.Tables(2) End Function 

But this is an error in this line: 'ds.ReadXml (reader)' with the following error:

 A column named 'comments' already belongs to this DataTable. 

Perhaps this does not work, since this code since 2003? Does anyone have a sample working code? Thank you very much in advance!

+4
source share
6 answers

You can use LINQ to XML to read WordPress RSS feeds.

Get the feed first. Retrieve the Uri instance.

 var rssFeed = new Uri("http://cgeers.com/feed/"); 

Then execute the GET request.

 var request = (HttpWebRequest) WebRequest.Create(rssFeed); request.Method = "GET"; var response = (HttpWebResponse) request.GetResponse(); 

Get the response stream and read it to download the contents of the feed.

 using (var reader = new StreamReader(response.GetResponseStream())) { var feedContents = reader.ReadToEnd(); //... } 

In the using statement above, use LINQ to XML to parse the loaded content and extract the necessary information.

 var document = XDocument.Parse(feedContents); var posts = (from p in document.Descendants("item") select new { Title = p.Element("title").Value, Link = p.Element("link").Value, Comments = p.Element("comments").Value, PubDate = DateTime.Parse(p.Element("pubDate").Value) }).ToList(); 

Now you can iterate over the results.

 foreach(var post in posts) { Console.WriteLine(post.Title); Console.WriteLine(post.Link); Console.WriteLine(post.Comments); Console.WriteLine(post.PubDate); } 

Here I just used an anonymous type to input the output, but feel free to create your own BlogPost class or something similar that you can use in your LINQ query.

I'm used to C #, so I used it in my answer. But you can easily convert it. There are several online converters that you can use.

As for your problem with the DataSet (which I personally would not use to implement this), this is because the element (blog post) has nodes with the same name.

For instance:

 <comments>...</comments> <slash:comments>5</slash:comments> 

Of course, the second one has a different namespace (slash), but the DataSet ReadXml (...) method is not interested in the namespace. He is trying to create a second column called "comments". That is why you get an exception.

You can still use a DataSet / DataTable if you want. Just retrieve the data from the feed using LINQ to XML as shown above.

Then create a DataSet and add a new table to it.

 var dataSet = new DataSet(); var blog = new DataTable("Blog"); blog.Columns.Add("Title", typeof(string)); blog.Columns.Add("Link", typeof(string)); blog.Columns.Add("Comments", typeof(string)); dataSet.Tables.Add(blog); 

Iterate over the extracted data and add it to the DataTable:

 foreach (var post in posts) { var newRow = blog.NewRow(); newRow["Title"] = post.Title; newRow["Link"] = post.Link; newRow["Comments"] = post.Comments; blog.Rows.Add(newRow); } 

Voila, we have now fixed your problem by no longer relying on the DataSet ReadXml (...) method. Download the feed, extract the data you are interested in and save it.

+17
source

I would start in the System.ServiceModel.Syndication , there are classes that can directly manipulate RSS feeds. In particular, this looks promising:

  XmlReader reader = XmlReader.Create("http://your.uri.here/feed.xml"); SyndicationFeed feed = SyndicationFeed.Load(reader); 

Then we examine the SyndicationFeed class, in particular, the Items collection should contain RSS entries.

+5
source

I would start with the built-in classes for RSS / Atom: SyndicationFeed

 using (XmlReader reader = XmlReader.Create(url)) { return SyndicationFeed.Load(reader); } 
+3
source

This is just a great @Christophe Geers solution converted to VB as a function:

 Protected Function getWordPressFeed(ByVal strUrl As String) As DataTable Dim rssFeed = New Uri(strUrl) Dim request = DirectCast(WebRequest.Create(rssFeed), HttpWebRequest) request.Method = "GET" Dim response = DirectCast(request.GetResponse(), HttpWebResponse) Dim feedContents As String Using reader = New StreamReader(response.GetResponseStream()) feedContents = reader.ReadToEnd() End Using Dim document = XDocument.Parse(feedContents) Static Dim dcNamespace As XNamespace dcNamespace = "http://purl.org/dc/elements/1.1/" Dim posts = (From p In document.Descendants("item") Select New With { _ Key .Title = p.Element("title").Value, _ Key .Link = p.Element("link").Value, _ Key .Author = p.Element(dcNamespace + "creator").Value, _ Key .Description = p.Element("description").Value, _ Key .PubDate = DateTime.Parse(p.Element("pubDate").Value) _ }).ToList() Dim dataSet = New DataSet() Dim blog = New DataTable("Blog") blog.Columns.Add("Title", GetType(String)) blog.Columns.Add("Link", GetType(String)) blog.Columns.Add("Description", GetType(String)) blog.Columns.Add("Author", GetType(String)) blog.Columns.Add("PubDate", GetType(DateTime)) dataSet.Tables.Add(blog) For Each post In posts Dim newRow = blog.NewRow() newRow("Title") = post.Title newRow("Link") = post.Link newRow("Description") = post.Description newRow("Author") = post.Author newRow("PubDate") = post.PubDate blog.Rows.Add(newRow) Next Return blog End Function 
0
source

This is what I use for my Wordpress feed reader.

 private async void ReadFeed() { var rssFeed = new Uri("http://truestrengthmd.com/category/blog/feed"); var request = (HttpWebRequest)WebRequest.Create(rssFeed); request.Method = "GET"; var _response = await request.GetResponseAsync(); var response = (HttpWebResponse)_response; using (var reader = new StreamReader(response.GetResponseStream())) { var feedContents = reader.ReadToEnd(); var document = XDocument.Parse(feedContents); var posts = (from p in document.Descendants("item") select new { Title = p.Element("title").Value }).ToList(); foreach (var post in posts) { Debug.WriteLine(post.Title); } } } 
0
source

You can use my library for this: wprssapi.marcogriep.de

Just a few lines of code. Very easy to do:

 //Get an Instance of Wordpress Controller (Singleton) WordPressFeedController wp = WordPressFeedController.Instance; //Load all the RSS Articles wp.LoadRSS("http://www.its-all-about.de/rss"); //Get the Newest Article (Check Docs for other functions) var rssItem = wp.GetNewestItem(); this.label1.Text = rssItem.Title; //Text Only, Remove all the HTML Tags - Limit too 300 Chars this.richTextBox1.Text = wp.RemoveHTMLFromText(rssItem.Summary.Substring(0, 300)) + "..."; //Open RSS Article on Button Click this.button1.Click += (s, e) => { Process.Start(rssItem.Id); }; 
0
source

All Articles