Click here to Skip to main content
6,597,576 members and growing! (23,431 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

ATOM To RSS Converter

By Govardhana Reddy

This article will help you to find a way to convert the ATOM to RSS Feed Format
C# 1.0, C# 2.0, Windows, .NET 1.1, .NET 2.0, ASP.NET, WebForms, VS.NET2003, VS2005, Dev
Posted:24 Oct 2007
Views:6,473
Bookmarked:5 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
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

4
1 vote, 20.0%
5

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, there 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 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 only tried to extract Title, Description, Time, Author, and Link. You can extend that at any point of time. I have even tried to save the converted content. If you wish, you can remove that statement in the code.

History

  • 24th October, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Govardhana Reddy


Member
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
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralWhy anybody would want to do it? Pinmemberchopeen21:57 24 Oct '07  
RantRe: Why anybody would want to do it? PinmemberRiaan Lehmkuhl1:00 19 Jul '08  

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: Deeksha Shenoy
Copyright 2007 by Govardhana Reddy
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project