Are you looking for customized advices? Click here to contact the expert!
by
2
6
4,575
2
Top 1% !
Popular
Famous
Easy-to-find
Tuned
OpenSource
Popularity: 1856th place
Created
Modified Nov 4, 2014

Published on:

.NETFramework4.0
Architecturex86_64
ClassNameRssReader
Extension.cs
InputDefValue2false
InputName0url
InputName1maxItems
InputName2stripHtml
InputType0string
InputType1int
InputType2bool
Languagecsharp
LicenseMIT_X11
MethodNameReadRss
OSWindows
OutputTypeIList<RssItem>
OutputTypeIList<RssReader.RssItem]
RequiredLibSystem.ServiceModel
RequiredSnippet215953
usingSystem.ServiceModel.Syndication
Windows VersionWindows7

Read the content of an rss feed

Retrieves the RSS Item at the specified URL and prepares a list of RSS Items with tile, summary and id. The caller should specify the maximum number of items to download and can decide to strip HTML from title and summary.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/215957/Read-the-content-of-an-rss-feed?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public class RssReader { public class RssItem { public string Title { get; set; } public string Summary { get; set; } public string Id { get; set; } } /// <summary> /// Requires System.ServiceModel to be added as a reference /// </summary> /// <param name="url"></param> /// <param name="maxItems"></param> public static IList<RssItem> ReadRss(string url, int maxItems, bool stripHtml = false) { IList<RssItem> result = new List<RssItem>(); XmlReader reader = XmlReader.Create(url); SyndicationFeed feed = null; try { feed = SyndicationFeed.Load(reader); } catch { return result; } finally { reader.Close(); } int totCount = 0; foreach (SyndicationItem item in feed.Items) { if (totCount++ > maxItems) break; string subject = (item.Title == null ? "" : item.Title.Text); string summary = (item.Summary == null ? "" : item.Summary.Text); if (stripHtml) summary = HtmlHelper.RemoveHtmlTag(HtmlHelper.RemoveHtmlTag(HtmlHelper.RemoveHtmlTag(HtmlHelper.RemoveHtmlTag(HtmlHelper.RemoveHtmlTag(summary, "div"), "a"), "img"), "table"), "br"); string id = item.Id; result.Add(new RssItem() { Title = subject, Summary = summary, Id = id}); } return result; } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus