C# - How to get full text content from rss feed? - c#

I want the full content from a rss feed, not just the description.
This is what I have:
string RssFeedUrl = "http://g1.globo.com/dynamo/rss2.xml";
List<feed> feeds = new List<feed>();
try
{
XDocument xDoc = new XDocument();
xDoc = XDocument.Load(RssFeedUrl);
var items = (from x in xDoc.Descendants("item")
select new
{
title = x.Element("title").Value,
link = x.Element("link").Value,
pubDate = x.Element("pubDate").Value,
description = x.Element("description").Value
});
if (items != null)
{
foreach (var i in items)
{
feed f = new feed
{
Titulo = i.title,
Link = i.link,
DataPublicada = i.pubDate,
Descricao = i.description
};
feeds.Add(f);
}
}
gvRss.DataSource = feeds;
gvRss.DataBind();
}
catch (Exception ex)
{
throw;
}
It is just retrieving me a short excerpt, but I want the full content text.

Related

XDocument get Element value with Namespace (RSS Feed)

Im working on getting some values from an RSS feed but i am having difficulties getting a value which has the namespace in the element tag. I've tried adding the namespace to the lookup of the value but i always get null
Any idea on how this is achieved?
Feed
https://wegotthiscovered.com/movies/feed/
Element
xmlns:content="http://purl.org/rss/1.0/modules/content/"
Namespace
content:encoded
public async Task<bool> GetNewsFeeds()
{
Database db = new Database();
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("https://wegotthiscovered.com/movies/feed/", "Movie");
dictionary.Add("https://wegotthiscovered.com/blu-ray/feed/", "Blu-ray");
dictionary.Add("https://wegotthiscovered.com/reviews/feed/", "Reviews");
dictionary.Add("https://wegotthiscovered.com/featured/feed/", "Featured");
dictionary.Add("https://wegotthiscovered.com/galleries/feed/", "Galleries");
db.DeletMovieNews();
foreach (var pair in dictionary.ToList())
{
try
{
if (PhysicalDevice.HasInternetConnection())
{
XDocument doc = XDocument.Load(pair.Key);
XNamespace nsSys = "http://purl.org/rss/1.0/modules/content/";
var entries = (from item in doc.Descendants("item")
select new Movie_News
{
Content = item.Element(nsSys + "encoded").Value, // ISSUE HERE
Link = item.Element("link").Value,
PublishedDate = item.Element("pubDate").Value,
Title = item.Element("title").Value,
Description = item.Element("description").Value,
GroupName = "News",
FeedName = pair.Value
});
List<Movie_News> newsCollection = entries.ToList();
if (newsCollection.Count() != 0)
{
using (var rateGate = new RateGate(40, TimeSpan.FromSeconds(10)))
{
rateGate.WaitToProceed();
foreach (Movie_News item in newsCollection)
{
string regex = #"((http|ftp|https):\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?";
Match match = Regex.Match(item.Description, regex);
if (match.Success)
{
item.ImageUrl = match.Value;
item.B64Image = await DownloadImage(item.ImageUrl);
}
item.Description = item.Description.Remove(0, item.Description.IndexOf("</div>"));
item.Description = item.Description.Replace("</div>","");
db.InsertNewsData(item);
}
}
}
return true;
}
}
catch(Exception ex)
{
return false;
}
}
return true;
}
}
Typical , soon as i completed the write up, its working now

ASP.NET Web Page with AtomFeed RSS feeds loading time too long

Now I have code which is sync, I need to make it async.
I tried what Visual Studio 2012 offers, the code have no mistackes but with no result.
Please help.
Code:
protected void Page_Load(object sender, EventArgs e)
{
this.PopulateAtomFeed1();
this.PopulateAtomFeed2();
this.PopulateAtomFeed3();
this.PopulateAtomFeed4();
}
private void PopulateAtomFeed1()
{
string RssFeedUrl = "http://www.faktor.mk/feed/ekonomija/";
List<Feeds> feeds = new List<Feeds>();
try
{
XDocument xDoc = new XDocument();
xDoc = XDocument.Load(RssFeedUrl);
var items = (from x in xDoc.Descendants("item").Take(10)
select new
{
title = x.Element("title").Value,
link = x.Element("link").Value,
pubDate = x.Element("pubDate").Value,
description = x.Element("description").Value
});
if (items != null)
{
foreach (var i in items)
{
Feeds f = new Feeds
{
Title = i.title,
Link = i.link,
PublishDate = i.pubDate,
Description = i.description
};
feeds.Add(f);
}
}
gvRss1.DataSource = feeds;
gvRss1.DataBind();
}
catch (Exception ex)
{
feeds = null;
}
}

How to order an RSS Feed from a News Site by the Publication Date

I am using .net 4 and reading a RSS feed from a News website - http://feeds.bbci.co.uk/news/uk/rss.xml?edition=uk
I have got the feed coming into the page using a repeater but I have just noticed that the feeds are not sorting Publication Date DESC all the time. There seems to be a strange grouping.
How am I able to explicitly specify that it is sorted by Publication Date DESC?
This is what I have so far...
private void PopulateRssFeed()
{
//BBC UK
string RssFeedUrl = "http://feeds.bbci.co.uk/news/uk/rss.xml?edition=uk";
List<Feeds> feeds = new List<Feeds>();
try
{
XDocument xDoc = new XDocument();
xDoc = XDocument.Load(RssFeedUrl);
//Take 3 Limits the number of items to display
//i.e -
//var items = (from x in xDoc.Descendants("item").Take(3)
var items = (from x in xDoc.Descendants("item")
select new
{
title = x.Element("title").Value,
link = x.Element("link").Value,
pubDate = x.Element("pubDate").Value,
description = x.Element("description").Value
});
if (items != null)
{
foreach (var i in items)
{
Feeds f = new Feeds
{
Title = i.title,
Link = i.link,
PublishDate = i.pubDate,
Description = i.description
};
feeds.Add(f);
}
}
Repeater1.DataSource = feeds;
Repeater1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
Use LINQ orderby ... descending :
var items = (from x in xDoc.Descendants("item")
orderby (DateTime)x.Element("pubDate") descending
select new
{
title = x.Element("title").Value,
link = x.Element("link").Value,
pubDate = x.Element("pubDate").Value,
description = x.Element("description").Value
});
As an aside, items can never be null so if (items != null) check is not needed.

The name 'xDocument' does not exist in the current context

I get an error "The name 'xDocument' does not exist in the current context" when writing some code for RSS feed reader. What is wrong here with xDocument?
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
namespace RSSreaderAPP
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReadRss();
}
}
private void ReadRss()
{
string RssFeedUrl = "http://feeds.feedburner.com/neowin-main";
List<Feeds> feeds = new List<Feeds>();
try
{
XDocument xDoc = new XDocument();
xDoc = xDocument.Load(RssFeedUrl);
var items = (from x in xDoc.Descendants("item")
select new
{
title = x.Element("title").Value,
link = x.Element("link").Value,
pubDate = x.Element("publish date").Value,
description = x.Element("description").Value
});
if (items != null)
{
foreach (var i in items)
{
Feeds f = new Feeds
{
Title = i.title,
Link = i.link,
PublishDate = i.pubDate,
Description = i.description
};
feeds.Add(f);
}
}
gridViewRSS.DataSource = feeds;
gridViewRSS.DataBind();
}
catch (Exception ex)
{
throw;
}
}
}
}
This is to avoid annoying message.
I changed
XDocument xDoc = new XDocument();
xDoc = xDocument.Load(RssFeedUrl);
to
XDocument xDoc = XDocument.Load(RssFeedUrl);
and also pubDate = x.Element("publish date").Value,
to
pubDate = x.Element("pubDate").Value,
private void ReadRss()
{
string RssFeedUrl = "http://feeds.feedburner.com/neowin-main";
List<Feeds> feeds = new List<Feeds>();
try
{
//XDocument xDoc = new XDocument();
//xDoc = xDocument.Load(RssFeedUrl);
XDocument xDoc = XDocument.Load(RssFeedUrl);
var items = (from x in xDoc.Descendants("item")
select new
{
title = x.Element("title").Value,
link = x.Element("link").Value,
pubDate = x.Element("pubDate").Value,
description = x.Element("description").Value,
});
if (items != null)
{
foreach (var i in items)
{
Feeds f = new Feeds
{
Title = i.title,
Link = i.link,
PublishDate = i.pubDate,
Description = i.description,
};
feeds.Add(f);
}
}
gridViewRSS.DataSource = feeds;
gridViewRSS.DataBind();
}
catch (Exception ex)
{
throw;
}
}

Removing Invalid Characters from XML within XDocument

I am trying to write music Tags into an XML, but it fails on invalid chars, I have tried doing a replace but I can't seem to get the syntex right.
//string pattern = "[\\~#%&*{}/:<>?|\"-]";
//string replacement = "_";
//Regex regEx = new Regex(pattern);
//string sanitized = Regex.Replace(regEx.Replace(input, replacement), #"\s+", " ");
XDocument baddoc = new XDocument
(new XElement("Corrupt",
badfiles.Select(badfile =>
new XElement("File", badfile))));
baddoc.Save("D:\\badfiles.xml");
// foreach(string musicfile in musicfiles)
//{ String Title = (TagLib.File.Create(musicfile).Tag.Title); }
XDocument doc = new XDocument
(new XElement("Songs",
musicfiles.Select(musicfile=>
new XElement("Song",
(new XElement("Title", (TagLib.File.Create(musicfile).Tag.Title))),
(new XElement("Path", (musicfile))),
(new XElement("Artist", (TagLib.File.Create(musicfile).Tag.Performers)))
))));
doc.Save("D:\\files.xml");
I ended up breaking it all out like this:
XDocument doc = new XDocument();
XElement songsElement = new XElement("Songs");
foreach(var musicfile in musicfiles)
{
XElement songElement = new XElement("Song");
string songTitle;
try { songTitle = (TagLib.File.Create(musicfile).Tag.Title); }
catch { songTitle = "Missing"; }
uint songTNint;
try { songTNint = (TagLib.File.Create(musicfile).Tag.Track); }
catch { songTNint = 00; }
string songTN = songTNint.ToString();
string songPath = musicfile;
string songArtist;
try {songArtist = (TagLib.File.Create(musicfile).Tag.Performers[0]);}
catch {songArtist = "Missing";}
List<string> songGenres = new List<string>();
foreach (string Genre in (TagLib.File.Create(musicfile).Tag.Genres))
{ songGenres.Add(Genre);}
string songGenre;
if (songGenres.Count > 1) { songGenre = (songGenres[0] + "/" + songGenres[1]); }
else { try { songGenre = songGenres[0]; } catch { songGenre = "Missing"; } }
songArtist = Regex.Replace(songArtist, #"[^\u0020-\u007E]", string.Empty);
XElement titleElement = new XElement("Title",songTitle);
XElement tnElement = new XElement("TN", songTN);
XElement pathElement = new XElement("Path", musicfile);
XElement artistElement = new XElement("Artist",songArtist);
XElement genreElement = new XElement("Genre", songGenre);
songElement.Add(titleElement);
songElement.Add(tnElement);
songElement.Add(pathElement);
songElement.Add(artistElement);
songElement.Add(genreElement);
songsElement.Add(songElement);
}

Categories

Resources