Introduction
Some time ago Peter Bromberg wrote an article to convert an Atom feed to a RSS feed. In this code-snipp you will learn how to do the opposite - we will convert a simple RSS feed to an Atom feed.
RSS to Atom
As mentioned in the introduction, the original idea came with writing of an article by Peter Bromberg. The article can be found here. Like the code displayed in Peter's article this snipp uses the basic Xml classes for the conversion. The code is straight forward - I created a simple class, that you can use to convert the RSS feed to Atom.
Code
Imports System.IO
Imports System.Xml
Imports Microsoft.VisualBasic
Public Class FeedConverter
Public Shared Function ConvertRssToAtom(ByVal strRssFeedURL As String) As StringWriter
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(strRssFeedURL)
' title, link, date
Dim strTitle As String = xmlDoc.SelectSingleNode("rss/channel/title").InnerText
Dim strLink As String = xmlDoc.SelectSingleNode("rss/channel/link").InnerText
Dim dtUpdatedDate As DateTime = Convert.ToDateTime(xmlDoc.SelectSingleNode("rss/channel/pubDate").InnerText)
' author info:
Dim node As XmlNode = xmlDoc.SelectSingleNode("rss/channel/managingEditor")
Dim strManagingEditor As String = String.Empty
If Not node Is Nothing Then
strManagingEditor = node.InnerText
End If
Dim sw As StringWriter = New StringWriter
Dim ms As New MemoryStream
Dim atomWriter As New XmlTextWriter(sw)
atomWriter.Formatting = Formatting.Indented
atomWriter.WriteStartDocument(True)
atomWriter.WriteStartElement("feed")
atomWriter.WriteAttributeString("xmlns", "http://www.w3.org/2005/Atom")
' write title, link and date
atomWriter.WriteElementString("title", strTitle)
atomWriter.WriteElementString("link", strLink)
atomWriter.WriteElementString("updated", dtUpdatedDate.ToString("s"))
' write author info
atomWriter.WriteStartElement("author")
atomWriter.WriteElementString("name", strTitle)
atomWriter.WriteElementString("email", strManagingEditor)
atomWriter.WriteEndElement()
atomWriter.WriteElementString("id", strLink) ' RSS feeds doesnt really have any id element
Dim gNode As XmlNode = Nothing
For Each entryNode As XmlNode In xmlDoc.SelectNodes("rss/channel/item")
atomWriter.WriteStartElement("entry")
atomWriter.WriteElementString("title", entryNode.SelectSingleNode("title").InnerText)
atomWriter.WriteElementString("link", entryNode.SelectSingleNode("link").InnerText)
gNode = entryNode.SelectSingleNode("guid")
If Not gNode Is Nothing Then
atomWriter.WriteElementString("id", gNode.InnerText)
Else
atomWriter.WriteElementString("id", entryNode.SelectSingleNode("link").InnerText)
End If
atomWriter.WriteEndElement()
Next
atomWriter.WriteEndElement()
atomWriter.WriteEndDocument()
Return sw
End Function
End Class
Usage
To convert the class simply use the ConvertRssToAtom class and you are ready to go. Here an example:
Response.ContentType = "text/xml"
Response.Write(FeedConverter.ConvertRssToAtom("http://blogs.law.harvard.edu/tech/xml/rss.xml").ToString)
Response.End()
Summary
This quick code-snippet explained how you can convert an RSS feed to ATOM.
Downloads
Class AtomToRssConverter
About Sonu Kapoor
 |
Sonu Kapoor is the creator of DotNetSlackers. Originally started in 2005, DotNetSlackers is a website to help developers stay up-to-date with the latest developments in .NET. During the sites’ youth DotNetSlackers focused solely on .NET news aggregation; however, over the years DotNetSlackers has gr...
View complete profile
|
Top Articles in this category
Reporting XML data using Crystal Reports and Windows Forms
This article will show you how to report on XML data with Crystal Reports and Windows Forms client.
XML Encryption
XML Encryption is a W3C standard for encrypting XML elements. The encryption process involves taking an element from an xml document, encrypting it and it's children, and then replacing the original XML content with the generated encrypted XML in such a way as the document remains well formed.
Dynamic XML from SQL Server
"Efficiency in simplicity", that's what XML is all about. XML is great for information exchange because of its simple flat file structure and user defined tags. For any application to interact with another either an complex marshalling code would be required or a simple implementation of XML would suffice. MS SQL gives us the advantage of generating dynamic XML in our data queries itself. In this article we will see some of the common used SQL XML queries.
Easy XML to SQL Using Linq
Richard J. Dudley shows how to transform XML to SQL using Linq.
|
|
Please login to rate or to leave a comment.