CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Grant Killian's Blog

No, this has nothing to do with beer -- but maybe it should?

Get To Know XPath

Piggy backing on the XML discussion we had in class last night, let me for the record provide Don Box's post on XML and COM.  If you only track one blog (besides mine, of course!), I'd make a strong case for Don's based on the depth of information he presents; some of it verges towards the academic, but it is always detailed and interesting.

Our class curriculum unfortunately skirts around XML, since the certification exam doesn't require much understanding of the topic; I hope most people don't care about only what's on the exam . . . and actually intend to truly learn .Net -- otherwise, you might get a job due to your certification, but you'll lose your job because you can't back it up.

That being said, let me introduce XPath; XPath is a W3C recommendation (which means it's widely used at this point -- and not a Microsoft proprietary thing) for working with XML data.  We use it, for example, on WeProgram.Net to dynamically load the latest blog headlines from our members. 

The following vb.Net code, while in C# on WeProgram.Net, is taken from my work there.  If you put a Button and a ListBox onto a windows form, and place this in the button click event, you can see how XPath works:

        dim xDoc as New Xml.XPath.XPathDocument( “http://dotnetjunkies.com/weblog/grant.killian/Rss.aspx” )
        dim xNav as Xml.XPath.XPathNavigator = xDoc.CreateNavigator()
        dim xIterator as Xml.XPath.XPathNodeIterator = xNav.Select( "/rss/channel/item/title" )
        while xIterator.MoveNext()
            listbox1.Items.Add( xIterator.Current.Value )
        End While

You'll see a list of Titles from my recent posts in the listbox; in a few short lines of code, we connect to and queried an internet data repository (served up as RSS in this case).  When we construct our XPathDocument, we pass it an http address for the raw data.  The Select() statement on the XPathNavigator object, specifically, is where we query the contents of the RSS feed coming from this blog.  There's a lot of MSDN documentation on constructing XPath query statements; you'll find it no more difficult than SQL, but the syntax is very different.  The XPathNodeIterator lets us quickly and easily move across our data.  Microsoft's implementation of XPath has limitations, of course, usually when working with very large amounts of XML, but Xml.XPath is a great namespace to get to know. 

This sample could easily be extended to use a textbox to control the string argument we pass into the XPathDocument constructor; we could type in “http://www.gotdotnet.com/team/dbox/rss.aspx” as another RSS feed.  Going further, we could pull back more than just the posting title; we could grab the date, the url address, even the entire content of it.  You could get really fancy and tie a database into the system, perhaps storing blogs for whatever purpose you dream up.  Before you know it, you'll be rolling your own blog aggregator like SharpReader by Luke Hutteman.  Not incidentally, I use SharpReader to track the several dozen blogs I follow; I highly recommend it if you follow blogs at all.

Happy .Netting



Check out Devlicio.us!