5,316,870 members and growing! (15,816 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

ATOM To RSS Converter

By Govardhana Reddy

This article will help u to find a way to convert the ATOM to RSS Feed Format
C# 1.0, C# 2.0, C#, Windows, .NET, .NET 1.1, .NET 2.0, ASP.NET, VS.NET2002, VS.NET2003, VS2005, Visual Studio, Dev

Posted: 24 Oct 2007
Updated: 24 Oct 2007
Views: 2,367
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.93 Rating: 2.76 out of 5
1 vote, 20.0%
1
1 vote, 20.0%
2
2 votes, 40.0%
3
0 votes, 0.0%
4
1 vote, 20.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Today we know Feeds play a major role in sharing information. RSS makes it possible for people to keep up with their favorite web sites in an automated manner that's easier than checking them manually.

Along with these their came many other versions like RDF, ATOM. The code provided here can help in converting the RDF Feeds to the RSS Feed format.


Using the code

Here the code accepts the ATOM Content as the argument and then returns back the XML Content which is in the RSS Feed format

//

public string AtomToRssConverter(XmlDocument atomDoc)
        {
            XmlDocument xmlDoc = atomDoc;

            XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable);

            mgr.AddNamespace("atom", "http://purl.org/atom/ns#");

            const string rssVersion = "2.0";

            const string rssLanguage = "en-US";

            string rssGenerator = "RDFFeedConverter";

            MemoryStream memoryStream = new MemoryStream();

            XmlTextWriter xmlWriter = new XmlTextWriter(memoryStream, null);

            xmlWriter.Formatting = Formatting.Indented;

            string feedTitle = xmlDoc.SelectSingleNode("//atom:title", mgr).InnerText;

            string feedLink = xmlDoc.SelectNodes("//atom:link/@href", mgr)[2].InnerText;

            string rssDescription = xmlDoc.SelectSingleNode("//atom:tagline", mgr).InnerText;

            xmlWriter.WriteStartElement("rss");

            xmlWriter.WriteAttributeString("version", rssVersion);

            xmlWriter.WriteStartElement("channel");

            xmlWriter.WriteElementString("title", feedTitle);

            xmlWriter.WriteElementString("link", feedLink);

            xmlWriter.WriteElementString("description", rssDescription);

            xmlWriter.WriteElementString("language", rssLanguage);

            xmlWriter.WriteElementString("generator", rssGenerator);

            XmlNodeList items = xmlDoc.SelectNodes("//atom:entry", mgr);

            if (items == null)
                throw new FormatException("Atom feed is not in expected format. ");
            else
            {
                string title = String.Empty;

                string link = String.Empty;

                string description = String.Empty;

                string author = String.Empty;

                string pubDate = String.Empty;

                for (int i = 0; i < items.Count; i++)
                {
                    XmlNode nodTitle = items[i];

                    title = nodTitle.SelectSingleNode("atom:title", mgr).InnerText;

                    link = items[i].SelectSingleNode("atom:link[@rel='alternate']", mgr).Attributes["href"].InnerText;

                    description = items[i].SelectSingleNode("atom:content", mgr).InnerText;

                    author = items[i].SelectSingleNode("//atom:name", mgr).InnerText;

                    pubDate = items[i].SelectSingleNode("atom:issued", mgr).InnerText;

                    xmlWriter.WriteStartElement("item");

                    xmlWriter.WriteElementString("title", title);

                    xmlWriter.WriteElementString("link", link);

                    xmlWriter.WriteElementString("pubDate", Convert.ToDateTime(pubDate).ToUniversalTime().ToString(@"ddd, dd MMM yyyy HH:mm:ss G\MT"));

                    xmlWriter.WriteElementString("author", author);

                    xmlWriter.WriteElementString("description", description);

                    xmlWriter.WriteEndElement();
                }

                xmlWriter.WriteEndElement();

                xmlWriter.Flush();

                xmlWriter.Close();
            }

            XmlDocument retDoc = new XmlDocument();

            string outStr = Encoding.UTF8.GetString(memoryStream.ToArray());

            retDoc.LoadXml(outStr);

            retDoc.Save("c:\\gova.xml");

            memoryStream.Close();

            xmlWriter.Close();

            return outStr;
        }


//

Points of Interest

I have tried to extract only Title, Description, Time, Author, Link only u can at any point of time extend. I have even tried to save the converted content if u wish u can remove that statement in the code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Govardhana Reddy


This is Govardhana Reddy, i am here to explore this world of INTERNET. I feel this is one way through which i can explore this world of INTERNET.

I want to be one of the best in this profession (a Software Developer, not a Software Engineer its a bit Controversial.)

My definition of a Software Engineer : "A person who knows what to cut/copy and where to paste".

Anyways long road ahead...

You can find more articles on http://www.apondu.50megs.com

Occupation: Software Developer
Location: India India

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralWhy anybody would want to do it?memberchopeen21:57 24 Oct '07  
RantRe: Why anybody would want to do it?memberRiaan Lehmkuhl21hrs 4mins ago 

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Oct 2007
Editor:
Copyright 2007 by Govardhana Reddy
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project