Silverlight 4 introduces XPath support including XPathNavigator and LINQ to XML extension methods for evaluating XPath expressions on your XElement objects! For those that aren’t familiar with XPath, it is essentially a big string that contains specially formatted text that is interpreted at runtime to describe a pattern for finding nodes in an XML document. You can use it to find nodes with specific conditions much like how regular expressions finds smaller strings in a bigger string.
The downsides to using XPaths are 1) the string describing your XPath can get quite complicated and difficult to read, and 2) because the string must be interpreted at runtime, performance is worse than alternate methods. This is why I normally discourage using XPaths and probably why Microsoft shyed away from including XPath in earlier versions of Silverlight… thereby bolstering using the better practice of LINQ to XML for searching and parsing XML documents. However, XPaths (like regular expressions) have their place and at times come in very handy!
One particular use that I’ve come to love over the years is the ability to create and run XPath queries at runtime. You don’t have to know very much about XPaths to easily write simple ones like “/vehicles/car” to find all the ‘car’ nodes in the ‘vehicles’ node. Or slightly more complex XPaths like: “/vehicles/car[@color=’green’]” (find all car nodes with a ‘color’ attribute of ‘green’). Because an XPath is just a string, it is very easy to assemble at runtime or even allow a power users to enter it in the UI if you dare open that can of worms 🙂 However, there is one kind of user I feel perfectly comfortable allowing in the can of worms… developers. Which is why I’ve created…
XPathPad
How often do you just want to find out very simple information about an Xml file? Maybe you want to know how many nodes are in your document or how many have a certain attribute. Maybe you want to extract the text values from certain nodes so you can use them in another application. Maybe you want to paste all the values of a certain attribute into Excel so you can manually graph them. The possibilities are endless and if you ever have to work with Xml files (especially someone else’s), a tool to easily help without writing a program can be invaluable!
How to use it:
- Run the app. Note: You can also install Out of browser.
- Browse for any xml file…
- Type in an XPath for the nodes you want to find
- Type in another XPath in the Output field for what stuff in each of those nodes you want to display.
- Click Go
Note: You can optionally enter a comma delimited list of XPaths in the output field if you want to get multiple things about each node found. Multiple results per node are separated by tabs so you can easily copy and paste the results into Excel and get different columns for each.
How it works under the hood:
Silverlight 4’s new XPath features did just about all of the work for me. First, I had to add a reference to the new XPath assembly:
Then, I simply load a standard XDocument from a filestream and call the new extension method that is available:
IEnumerable<XElement> elements = xdoc.XPathSelectElements(XPath);
This returns all the elements found at the provided XPath. I then loop thru each of these elements and call a second extension method to get a node relative to that element based on the output XPath:
var result = element.XPathEvaluate(outputXPath) as IEnumerable;
XObject value = result.Cast<XObject>().FirstOrDefault();
if (value is XElement)
return ((XElement)value).Value;
else if (value is XText || value is XCData)
return ((XText)value).Value;
else if (value is XAttribute)
return ((XAttribute)value).Value;
else
return value.ToString();
XPathEvaluate returns a collection of XObjects. I simply take the first one and return its value to be displayed in the results window.
To find out more about how XPathPad was written or to modify it to better fit your needs, download the source.