tags:

views:

233

answers:

4

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 Content 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

A: 

Does it have anything to do with the type of rss feed you're consuming?

http://codex.wordpress.org/WordPress_Feeds

hunter
Hmm, I've read up on your link.. seems I'm getting data from the right rss. But I may still be wrong though - gotta read a little more about it. Thanks :)
bomortensen
A: 

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.

Ten Ton Gorilla
Thanks TTGorilla ;) Sadly, it didn't get formatted the way I wanted to. Thought I'd make this post because, to me, it seems a little overexaggarated (spell?) to make a whole new class to convert \n to < br />But.. it works ;)
bomortensen
A: 

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.

Mikael Svenson
Hi Mikael,Thanks for your comment :) I'm not that much for the <pre> tag, while it certainly is good in some ways it will not work for this. I tried it out and it formats the text in such a way that I could not be overridden with css :/ Think i'll stick to my current solution.
bomortensen
I'm not much for <pre> myself either, hehe. Just thought I'd mention it. Your solution is much better, and it's clean. Formatting tags shouldn't exist in xml structures imo :)
Mikael Svenson
+1  A: 

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.

Andy