mhinze.com archive

this is an archive of the old blog, ended 6/16/08





    23
    Feb

    OPML to HTML

    i was asked yesterday what blogs i read and i wanted to use data from my reader's opml document. but the converters I found weren't good enough, either they were javascript (ie: it didn't render source i could copy/paste into an email or blogpost) or they didn't include support for nested outlines, which is how google reader exports opml and also how i wanted to present it. so i took an hour this morning to write a asp.net page in c# that converts opml into html with support for nested outlines (folders, tags) and which renders nicely formatted unordered lists. i chose not to do xslt. ymmv.

    <%@ Page Language="C#" EnableViewState="false" ValidateRequest="false" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
        StringBuilder sb;
        protected void submit_Click(object sender, EventArgs e)
        {
            opml.Rows = 5;
            System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
            sb = new StringBuilder();
            try
            {
                xml.LoadXml(opml.Text);
            }
            catch
            {
                output.Text = "<h1>Invalid input</h1>";
                return;
            }
            System.Xml.XmlNode titleNode = xml.SelectSingleNode("opml/head/title");
            if (titleNode != null)
            {
                string title = titleNode.InnerText;
                sb.AppendFormat("<h1>{0}</h1>\n", title);
            }
            //process top level "folders"
            convertNodes(xml.SelectNodes("opml/body/outline"));
            output.Text = sb.ToString();
        }
        protected void convertNodes(System.Xml.XmlNodeList nodes)
        {
            sb.Append("<ul>\n");
            foreach (System.Xml.XmlElement element in nodes)
            {
                sb.Append("<li>");
                sb.Append(genLinkFromElement(element));
                if (element.HasChildNodes) convertNodes(element.ChildNodes);
                sb.Append("</li>\n");
            }
            sb.Append("</ul>\n");
        }
        protected string genLinkFromElement(System.Xml.XmlElement element)
        {
            string text = element.HasAttribute("text") ? element.GetAttribute("text") : "unknown description";
            string htmlLink = element.HasAttribute("htmlUrl") ?
                "<a href=\"" + element.GetAttribute("htmlUrl") + "\">" + text + "</a>" : text;
            string rssLink = element.HasAttribute("xmlUrl") ?
                "- [<a href=\"" + element.GetAttribute("xmlUrl") + "\">rss</a>]" : string.Empty;
            return string.Format("{0} {1}", htmlLink, rssLink);
        }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>convert OPML to HTML.. paste your opml, get a blogroll</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="opml" TextMode="multiline" Columns="100" Rows="40" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="submit" runat="server" Text="Process OPML" OnClick="submit_Click" />
            <asp:Literal ID="output" runat="server"></asp:Literal>
        </div>
        </form>
    </body>
    </html>

    3 Responses to “OPML to HTML”

    1. Randy Charles Morin Says:

      Good work!

    2. azwa Says:

      erm, how do i use this?

    3. Matt Says:

      asp.net

    Leave a Reply

    You must be logged in to post a comment.

    © 2007 mhinze.com