Created
January 31, 2012 12:17
-
-
Save warrenbuckley/1710234 to your computer and use it in GitHub Desktop.
Umbraco V5 Example - Fetch Remote RSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inherits PartialViewMacroPage | |
@using Umbraco.Cms.Web.Macros | |
@* Added these in *@ | |
@using System.ServiceModel.Syndication | |
@using System.Xml; | |
@{ | |
// Get the remote url | |
var remoteUrl = String.IsNullOrEmpty(Model.MacroParameters.remoteURL) ? "http://feeds.feedburner.com/umbracoblog" : Model.MacroParameters.remoteURL; | |
// Get the web request | |
var request = WebRequest.Create(remoteUrl); | |
// Set the timeout to 3 seconds (3000ms) | |
request.Timeout = 3000; | |
// Let's put it in a try/catch because the web request could throw and error (timeout, 500, etc) | |
try | |
{ | |
using (var response = request.GetResponse()) | |
{ | |
using (var stream = response.GetResponseStream()) | |
{ | |
if (stream != null) | |
{ | |
var reader = XmlReader.Create(stream); | |
var feed = SyndicationFeed.Load(reader); | |
foreach (SyndicationItem item in feed.Items) | |
{ | |
var link = item.Links.FirstOrDefault().GetAbsoluteUri(); | |
var description = item.ElementExtensions.ReadElementExtensions<string>("encoded", "http://purl.org/rss/1.0/modules/content/").FirstOrDefault(); | |
var pubDate = item.PublishDate; | |
<div class="rssItem"> | |
<h1><a href="@link">@item.Title.Text</a></h1> | |
<em>@pubDate.ToString("dd/MM/yy @ HH:mm")</em> | |
@Html.Raw(description) | |
</div> | |
} | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
// Log the error or do something with it... | |
} | |
} |
@jorgelusar thanks for your much better version using the .NET 4 SyndicationClass. I have updated the example.
@warrenbuckley Actually thank YOU for all your help and for all this examples :) #h5yr
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Warren, I think I'd use a different approach here. I'd use the System.ServiceModel.Syndication namespace and would retrieve the data using the SyndicationFeed.Load method. I would also set a timeout and put the logic in a try/catch because the web request could throw and error (timeout, 500, etc). Example: