While not an experienced developer I have been creating extensions and modifying code for some time… Always learning, I run into challenges that require extensive hunting for samples to help overcome the absence of knowledge. As was the case when I started working on the Wordle Widget. I really wanted the ability to display Wordles on my blog and figured other people might enjoy it as well.
My first challenge is that Wordle doesn’t provide any API hooks to gather information. Fortunately I discovered the Atom feed that will return an XML stream that contains the latest Wordles in the gallery. Adding a username to the filter of the Atom feed allows you to filter what Wordles are returned. So the first hurdle is overcome; we can use the Atom feed.
So how can I process XML data from a website? I found an excellent example of how to accomplish this using the Twitter Widget. A little cut and paste and I was able to get the core components of the widget drawn up. All great except a problem came up immediately… The XML document could not parse the nodes correctly. No matter what I did the SelectChildNodes(nodeName) produced a zero records list. It took a little time to figure out what is going on. Turns out, in order to parse the Atom feed correctly you need to declare and configure the XML namespace. It looks something like the following:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "http://www.w3.org/2005/Atom");
Now that I have added the Atom namespace I can use the SelectChildNodes method to gather all of the Wordle Entries. It’s important to note that I had to use the ‘bk:entry’ syntax to get a valid match, so don’t forget!
XmlNodeList items = root.SelectNodes("bk:entry", nsmgr);
List<Wordle> wordles = new List<Wordle>();
int count = 0;
for (int i = 0; i < items.Count; i++)
{
if (count == maxItems)
break;
XmlNode node = items[i];
Wordle wordle = new Wordle();
wordle.Title = Server.HtmlEncode(node.SelectSingleNode("bk:title", nsmgr).InnerText);
wordle.Url = new Uri(node.SelectSingleNode("bk:link", nsmgr).Attributes.Item(0).InnerText);
wordles.Add(wordle);
count++;
}
With that I was able to get my Wordle User control working and configured as a widget. For a view of the complete code feel free to download my widget and see how things work.




Kim Cameron's Identity Weblog
Mon, Aug 25, 2008
Technology