using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; namespace RssBanditToOpml { class Program { static void Main(string[] args) { string srcFile = null; string dstFile = null; Console.WriteLine("Convert RSS Bandit exported files to OPML format"); Console.WriteLine("Parameters: RSSConvert \"inputfile\" \"outputfile\""); Console.WriteLine(""); if (args.Length == 2) { srcFile = args[0]; dstFile = args[1]; if (File.Exists(srcFile)) { Console.Write("Converting..."); convert(srcFile, dstFile); Console.WriteLine("Done"); } else Console.WriteLine("Source file '{0}' does not exist.", srcFile); } else Console.WriteLine("Parameter Error"); Console.WriteLine(""); Console.Write("Hit a key..."); Console.Read(); } private static void convert(string srcFile, string dstFile) { XmlTextWriter dstXml = new XmlTextWriter(dstFile, System.Text.Encoding.ASCII); // utf-8 yields a byte order mark that grazr currently chokes on // Write Headers dstXml.Formatting = Formatting.Indented; dstXml.WriteStartDocument(); dstXml.WriteStartElement("opml"); dstXml.WriteAttributeString("version", "1.1"); dstXml.WriteStartElement("head"); dstXml.WriteStartElement("title"); dstXml.WriteString("RssBandit blogroll"); dstXml.WriteEndElement(); dstXml.WriteEndElement(); dstXml.WriteStartElement("body"); SortedDictionary outlines = new SortedDictionary(); XmlDocument doc = new XmlDocument(); doc.Load(srcFile); XmlNodeList outerOutlinenodes = doc.SelectNodes("/opml/body/outline"); createNodes(outlines, outerOutlinenodes); foreach (Outline outline in outlines.Values) { outline.SerializeToXml(dstXml); } dstXml.WriteEndElement(); dstXml.WriteEndElement(); dstXml.WriteEndDocument(); dstXml.Close(); } private static void createNodes(SortedDictionary outlines, XmlNodeList outlinenodes) { foreach (XmlNode outlinenode in outlinenodes) { string title = outlinenode.Attributes["title"].Value; outlines.Add(title, new Outline(title, outlinenode)); } } class Outline { #region fields and properties private string title; public string Title { get { return title; } set { title = value; } } SortedDictionary outlines = new SortedDictionary(); string type; string xmlUrl; #endregion #region ctors public Outline(string title, XmlNode outlinenode) { this.title = title; if (outlinenode.HasChildNodes) { XmlNodeList innerOutlinenodes = outlinenode.SelectNodes("outline"); createNodes(outlines, innerOutlinenodes); } else { type = "rss"; xmlUrl = outlinenode.Attributes["xmlUrl"].Value.ToString(); } } #endregion internal void SerializeToXml(XmlTextWriter dstXml) { dstXml.WriteStartElement("outline"); dstXml.WriteAttributeString("text", title); foreach (Outline outline in outlines.Values) { outline.SerializeToXml(dstXml); } if (type != null) { dstXml.WriteAttributeString("type", type); dstXml.WriteAttributeString("xmlUrl", xmlUrl); } dstXml.WriteEndElement(); } } } }