XLinq PowerShell Module to Wrap XLinq API

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

   1: Import-Module XLinq
   2:  
   3: $doc = `
   4:     XDoc `
   5:         (XDecl -encoding 'utf-16') `
   6:         (XElem root `
   7:             (XElem author `
   8:                 (XAttr fname Michael) `
   9:                 (XAttr lname Crichton) `
  10:                 (XElem books `
  11:                     (XElem book `
  12:                         (XAttr title Prey) `
  13:                     ) `
  14:                  ) `
  15:             ) `
  16:             (XElem author `
  17:                 (XAttr fname Jon) `
  18:                 (XAttr lname Skeet) `
  19:                 (XElem books `
  20:                     (XElem book `
  21:                         (XAttr title 'C# in Depth') `
  22:                     ) `
  23:                  ) `
  24:             ) `
  25:         )
  26:     
  27: $path = [IO.Path]::GetTempFileName()
  28: $doc.Save($path)
  29: Get-Content $path
  30: Remove-Item $path    

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).

psmdtag:module: XLinq
psmdtag:dotnet: Xml

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s