Xlinq and WordprocessingML

Last month I attended a meeting where Eric White described a really cool way he has been using the new Xlinq technology to iterate through sections of a WordprocessingML document and extract code snippets to be compiled and run for test cases. Xlinq provides the ability to do SQL-style queries on the contents of XML documents, which can really simplify the task of extracting nodes from a complex document.

I attended the meeting because my wife Megan works on Eric's team and she had told me about what Eric was doing. It looked very cool and I've been meaning to dig into this topic and play with it a bit, and then today I noticed (through Brian Jones's blog) that Eric has blogged about the code he demonstrated last month. Check it it out, he describes exactly how it works so that you can apply Xlinq to other programming tasks.

Here's an example from Eric's code, showing how easy it is to select all of the annotations from a WordprocessingML document using Xlinq:

var commentNodes =
from annos in wordDoc.Descendants(aml + "annotation")
where (string)annos.Attribute(w + "type") == "Word.Comment"
select annos;

After this code runs, commentNodes is a collection of the annotations and you can simply use foreach to iterate through them and do whatever you need to do.

Nicely done, Eric! More posts like this, please. :-)