Created
November 11, 2021 22:31
-
-
Save peevees/8cfe7ea513b6bd55712ec348c2dfe617 to your computer and use it in GitHub Desktop.
A quick example of reading RSS feeds in C#
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
using System; | |
using System.Linq; | |
using System.ServiceModel.Syndication; | |
using System.Xml; | |
namespace RssReader | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Rss start!"); | |
//Some example urls | |
//var url = "https://xkcd.com/atom.xml"; | |
//var url = "https://feeds.feedburner.com/Explosm"; | |
//var url = "https://www.minecraftforum.net/news.rss"; | |
//var url = "https://www.webtoons.com/en/challenge/books-of-adam/rss?title_no=136637"; | |
//var url = "https://feeds2.feedburner.com/little-gamers/XrRa"; | |
//var url = "http://services.runescape.com/m=news/latest_news.rss?oldschool=true"; | |
//var url = "http://services.runescape.com/m=news/latest_news.rss"; | |
//var url = "https://tapas.io/rss/series/5216"; | |
//var url = "https://www.webtoons.com/en/challenge/false-knees/rss?title_no=79544"; | |
//var url = "http://feeds.feedburner.com/satwcomic"; | |
//var url = "https://lifehacker.com/rss" | |
var url = "https://www.webtoons.com/en/slice-of-life/bluechair/rss?title_no=199"; | |
XmlReaderSettings settings = | |
new XmlReaderSettings | |
{ | |
XmlResolver = null, | |
DtdProcessing = DtdProcessing.Prohibit, | |
IgnoreWhitespace = true, | |
CheckCharacters = true, | |
CloseInput = true, | |
IgnoreComments = true, | |
IgnoreProcessingInstructions = true | |
}; | |
using var reader = XmlReader.Create(url, settings); | |
SyndicationFeedFormatter GenericFeedFormatter = null; | |
Atom10FeedFormatter atom = new Atom10FeedFormatter(); | |
Rss20FeedFormatter rss = new Rss20FeedFormatter(); | |
if (reader.ReadState == ReadState.Initial) | |
{ | |
reader.MoveToContent(); | |
} | |
// try either atom or rss reading | |
if (atom.CanRead(reader)) | |
{ | |
GenericFeedFormatter = atom; | |
} | |
if (rss.CanRead(reader)) | |
{ | |
GenericFeedFormatter = rss; | |
} | |
if (GenericFeedFormatter == null) | |
{ | |
//uho we found no way to read this 😢 | |
//System.Environment.Exit(1); | |
} | |
GenericFeedFormatter.ReadFrom(reader); | |
var feed = GenericFeedFormatter.Feed; | |
Console.WriteLine($"RSS title: {GenericFeedFormatter.Feed.Title.Text}"); | |
foreach (var item in GenericFeedFormatter.Feed.Items) | |
{ | |
Console.WriteLine($"Post id: {item.Id}"); | |
Console.WriteLine($"Post title: {item.Title.Text}"); | |
Console.WriteLine($"Post last updated time: {item.LastUpdatedTime}"); | |
Console.WriteLine($"Post publish date: {item.PublishDate}"); | |
Console.WriteLine($"Post authors: {item.Authors.FirstOrDefault()?.Name}"); | |
Console.WriteLine($"Post summary: {item.Summary?.Text}"); | |
Console.WriteLine($"Post summary: {((TextSyndicationContent)item.Content)?.Text}"); | |
Console.WriteLine($"Post baseurl: {item.BaseUri}"); | |
Console.WriteLine($"Post baseurl: {item.Links[0].Uri}"); | |
Console.WriteLine("\n"); | |
} | |
Console.WriteLine("rss end"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment