Skip to content

Instantly share code, notes, and snippets.

@judismith
Created April 29, 2024 01:31
Show Gist options
  • Save judismith/77fae4bcb686d61ad4c5603aa150be3b to your computer and use it in GitHub Desktop.
Save judismith/77fae4bcb686d61ad4c5603aa150be3b to your computer and use it in GitHub Desktop.
Parse RSS Feed from Wordpress Blog
using System.ServiceModel.Syndication;
using System.Xml;
using System.Xml.Linq;
string url = "https://shaolinarts.com/feed/rss/";
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
reader.Close();
foreach (SyndicationItem item in feed.Items)
{
var author = "";
foreach (SyndicationElementExtension extension in item.ElementExtensions)
{
XElement ele = extension.GetObject<XElement>();
if (ele.ToString().StartsWith("<dc:creator"))
{
var start = ele.ToString().IndexOf(">") + 1;
var end = ele.ToString().IndexOf("</dc:creator>");
author = ele.ToString().Substring(start, end - start);
}
}
Console.WriteLine();
Console.WriteLine();
var description = item.Summary.Text;
var first = description.IndexOf("src=\"") + "src=\"".Length;
var last = description.IndexOf("jpg\"", first);
var imgLink = description.Substring(first, last - first + 3);
var text = description.Substring(description.IndexOf("<p>") + 3, description.IndexOf("</p>") - description.IndexOf("<p>") - 3);
Console.WriteLine(item.PublishDate);
Console.WriteLine(item.Title.Text);
Console.WriteLine(author);
Console.WriteLine();
Console.WriteLine(imgLink);
Console.WriteLine(text);
}
@judismith
Copy link
Author

Parse an RSS feed from a WordPress blog using C# and .Net Core 8. Show the technique for getting the standard individual items in SyndicationItem. The solution also shows how to get the additional elements using SyndicationElementExtension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment