Best way to consume an RSS feed
I'm currently working on an ASP.NET Website where I want to retrieve data from an RSS feed. I can easily retrieve the data I want and get it to show in i.e. a Repeater control.
My problem is, that the blog (Wordpress) that I'm getting the RSS from uses \n
for linebreaks which I obviously can't use in HTML. I need to replace these \n
with a <br />
tag.
What I've done so far is:
SyndicationFeed myFeed = SyndicationFeed.Load(XmlReader.Create("urltofeed/"));
IEnumerable<SyndicationItem> items = myFeed.Items;
foreach(SyndicationItem item in items)
{
Feed f = new Feed();
f.Content = f.ConvertLineBreaks(item.Summary.Text);
f.Title = item.Title.Text;
feedList.Add(f);
}
rptEvents.DataSource = feedList;
rptEvents.DataBind();
Then having a Feed class with two properties: Title and Conte开发者_运维技巧nt and a helper-method to replace \n
with <br />
However, I'm not sure if this is a good/pretty approach to get data from an RSS feed?
Thanks in advance,
Bo
If you are adverse to all the xml parsing in your code you can also run the rss xml schema through xsd and generate a topic and feed class in you code.
This classes should serialize/deserialize to xml. This may be overkill but it's worked great for me when integrating with a standard xml api for a third party.
Does it have anything to do with the type of rss feed you're consuming?
http://codex.wordpress.org/WordPress_Feeds
If it's not returning from the RSS feed formatted as you wish, you have little other choice. That's definitely not a particuarly bad way.
This is a sane approach in my opinion.
Remember that RSS is a data format and not HTML. Replacing \n with
in order to get your wanted HTML is just something which has to be done every now and then. (Unless you want to use <pre>)
I find people stuffing way too much html in xml structures, not considering they will be used for other things than web pages. UI and data should be separated.
精彩评论