This is pretty early stuff but I thought I would share it anyway. I really like PowerShell as a .NET REPL. It is the first way I’ll try an experiment with the various .NET APIs. That doesn’t always work out and in the early CTPs of PowerShell I was having a hard time getting XLinq to work as I expected. So I wrote this XLinq.psm1 module to make it a bit easier. This does require CTP3 of Windows PowerShell v2.0 (or Windows 7 which has this version installed). Note: you really have to resist the urge to use commas to separate arguments. 🙂 Here’s an example usage:
Contents of file: XLinqExample.ps1
It turns out that I put in more line continuation characters than needed but it was easier to just put them on every line than to sit and try to figure out if it was needed or not. The following output is generated when the script above runs and uses the XLinq module functions like XDoc, XElem, XAttr, etc:
PS> C:\Users\Keith\Documents\PowerShell\XLinqExample.ps1 <?xml version="1.0" encoding="utf-16" standalone="yes"?> <root> <author fname="Michael" lname="Crichton"> <books> <book title="Prey" /> </books> </author> <author fname="Jon" lname="Skeet"> <books> <book title="C# in Depth" /> </books> </author> </root>
I’m not claiming this is the best way to manipulate XML in PowerShell, however if you want to do a quick experiment using XLinq this module comes in handy. As always, if you find any bugs please drop me a line.
Update 02/13/2009: Updated the module to handle pipeline input and scriptblocks (inspired by a similar module that Josh Einstein came up with).