Atom to RSS 2.0 converter - The C# way

This blog was on blogger but a few weeks ago it started to launch a java connection exception. It doesn’t allowed me to publish my blog at this address through ftp.

Then I decided to switch to WordPress so I extracted a backup from blogger in the Atom format and tried to import it, but WordPress only accepts RSS 2.0

I tried to find an out of the box solution to make this convertion between Atom and RSS but nothing. So this is my implementation in C#

 1:// -----------------------------------------------------------------------
 2:// Isaias Gonzalez <siderevs@gmail.com>
 3:// -----------------------------------------------------------------------
 4:
 5:using System.IO;
 6:using System.ServiceModel.Syndication;
 7:using System.Xml;
 8:
 9:namespace DeAtomizer
10:{
11:    public class AtomToRss2Converter
12:    {
13:        public void AtomToRss20(string atomFileName, string rssFileName)
14:        {
15:            ConvertToRss20(ReadFeed(atomFileName), rssFileName);
16:        }
17:
18:        private static void ConvertToRss20(SyndicationFeed feed, string rssFileName)
19:        {
20:            Rss20FeedFormatter rss2 = new Rss20FeedFormatter(feed);
21:            XmlWriter writer = XmlWriter.Create(rssFileName);
22:            rss2.WriteTo(writer);
23:
24:            // You can also use 
25:            // feed.SaveAsRss20(writer);           
26:        }
27:
28:        private static SyndicationFeed ReadFeed(string fileName)
29:        {
30:            StreamReader stream = new StreamReader(fileName);
31:            XmlReader reader = XmlReader.Create(stream);
32:            SyndicationFeed feed = SyndicationFeed.Load(reader);
33:            return feed;
34:        }
35:    }
36:}

And as you can see, I successfully converted the file.

Leave a Reply