Posts Tagged ‘feed’

Read Rss feed in c#

Posted: August 9, 2012 in ASP.net, C#
Tags: , , , , ,

In this Article I am going to explain how you can read rss xml file into c# . It is very easy , you just have to follow few steps to catch xml feed into your code .

Here i`ll show you how can catch the rss feed , then you can display it on your website as you want .

to implement it you need to understand the function i am writing below :-

public void TestFeed()
{
var NewsRSS = XDocument.Load(“https://dotnetgeekblog.wordpress.com/feed/”); // to load XML file in variable
var rssItemsList = from d in NewsRSS.Descendants(“item”)
select d;
int count = rssItemsList.Count(); //get the count of feed items
foreach (var rssItem in rssItemsList)
{
XElement pubdate = rssItem.Descendants(“pubDate”).FirstOrDefault(); //to get the published date of news
string publishDate = string.Empty;
if (pubdate != null)
{
publishDate = pubdate.Value;
DateTime time = DateTime.Now;
string dayofWeek = time.DayOfWeek.ToString();
string dayofmonth = time.Month.ToString();
string year = time.Year.ToString();
}
string title = rssItem.Descendants(“title”).FirstOrDefault() == null ? string.Empty : rssItem.Descendants(“title”).FirstOrDefault().Value; //to set values in variable
string link = rssItem.Descendants(“link”).FirstOrDefault() == null ? string.Empty : rssItem.Descendants(“link”).FirstOrDefault().Value;//to set values in variable
string description = rssItem.Descendants(“description”).FirstOrDefault() == null ? string.Empty : rssItem.Descendants(“description”).FirstOrDefault().Value;
string guid = rssItem.Descendants(“guid”).FirstOrDefault() == null ? string.Empty : rssItem.Descendants(“guid”).FirstOrDefault().Value;//to set values in variable
string summarry = string.Empty;
if (!string.IsNullOrEmpty(description))
{
int length = description.Length;
if (length > 200)
summarry = description.Substring(0, 199) + “…”;
else
summarry = description;
}
}
}

 

This is just an example . Now You Can Play With It As you Want 😉

Hope This Post Will Help you Somehow . . 🙂

Waiting For Your Valuable Feedback.