|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
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
IntroductionToday 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 codeHere 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 InterestI 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||