I'm a bit new to asp.net so please bear with me...
I am trying to read and display an Atom feed from a WordPress site.
Scrounging the web, I was able to put together the following code in the Codebehind:
XmlReader reader = XmlReader.Create(myURL);
SyndicationFeed feed = SyndicationFeed.Load(reader);
foreach (var item in feed.Items)
{
Response.Write(item.PublishDate.ToString("yyyy-MM-dd hh:mm tt"));
Response.Write("<br/>");
Response.Write(item.Title.Text);
}
reader.Close();
This works perfectly fine to display the date and time. Now here are the issues I need to solve:
1) Retrieving the Link....
Looking at the SyndicationFeed posting on MSDN I can see there is a Links property, but I can't figure out how I can retrieve the <link>
from the feed. Anyone know how to get this?
2) Limiting the number of output...
Right now, with the foreach()
it displays every single entry in the feed. Any ideas how I can limit it to only show the newest x number?
Can i do something like...
while (var item in feed.Items < 5)
{
Response.Write(item.PublishDate.ToString("yyyy-MM-dd hh:mm tt"));
Response.Write("<br/>");
Response.Write(item.Title.Text);
}
- Any ideas how I can limit it to only show the newest x number?
- retrieving the collection of
SyndicationLink
You could (improve as needed/null checks, etc.):
//Newest by date/time and take x (e.g. 5)
foreach (var item in feed.Items.OrderByDescending(i => i.PublishDate).Take(5))
{
//Get the Uris from SyndicationLink
var theLinks = item.Links.Select(l => l.Uri.ToString()).ToList();
//do something with them....
var foo = string.Join(",", theLinks);
....
}
Hth....