More XML with PowerShell
Posted on 8 Mar 2007 at 11:36
With help from shiny power, Thomas Lee gets serious with PowerShell, updating the XML order document and using the xml reader class.
function Process-XML ($doc="d:\foo\order.xml")
{
# create settings object and open document
$settings = new-object System.Xml.XmlReaderSettings
$doc = (resolve-path $doc).ProviderPath
$reader = [xml.xmlreader]::create($doc, $settings)
[int] $count = 0
# loop through all elements
while ($reader.Read())
{
$count++
}
"$count elements found"
}
In this example, PowerShell creates and opens the XML Reader, then loops through the file, element by element, and counts the elements found. To demonstrate this better, let's examine a neat example function called Dump-Doc. This example function was created by Bruce Payette, one of the PowerShell developers at Microsoft, as part of his PowerShell book (see www.manning.com/payette/payette_ch10.pdf for more details of this example and www.manning.com/payette for more details about this excellent book). You'll find Payette's Dump-Doc function at www.pcpro.co.uk/links/151dotnet1.
I've made one small change to this function, altering the default document name to d:\foo\order.xml. When you run this function, the output looks like this:
PSH [D:\foo]: DUMP-DOC
<Order>
<OrderHeader>
<CustomerID>
134545
</CustomerID>
<CustomerName>
PS Partnership
</CustomerName>
...
As in my earlier example, with Dump-Doc, the document (d:\foo\order.xml) is opened using an XML Reader - the main loop begins with While ($reader.Read()), which reads each element sequentially, then outputs the elements. Depending on what you're trying to achieve, each method has advantages. For most simple XML processing tasks, the DOM model wins out on sheer simplicity in terms of your code.
Help in PowerShell
You can access PowerShell's built-in help system via the Get-Help cmdlet or using the -? switch on any cmdlet. Interestingly enough, the Help displayed by PowerShell is stored as XML, using a format known as MAML (Microsoft Assistance Markup Language). The XML Help files are stored below PowerShell in %systemroot%\system32\WindowsPowerShell. For example, on my system they're in d:\windows\system32\windowspowershell\v1.0\en-us, and there are five of these files by default called:
Microsoft.PowerShell.Commands.Management.dll-Help.xml
Microsoft.PowerShell.Commands.Utility.dll-Help.xml
Microsoft.PowerShell.ConsoleHost.dll-Help.xml
Microsoft.PowerShell.Security.dll-Help.xml
System.Management.Automation.dll-Help.xml
All five files contain XML and are therefore text files, which you can, of course, edit to clarify ambiguities in the Help text.
If you're creating your own cmdlet, you can also create an associated Help file that PowerShell can display using the Get-Help cmdlet. The Help content that describes your cmdlet should include a description of its function and the syntax it uses, with full descriptions of any parameters, examples of usage and other related notes. To create your Help file, you'd need to first create a text file with a name similar to the assembly that contains the snap-in that registers your cmdlet. The name of the Help file must be in the following format: MySnapinAssemblyName.dll-Help.xml
The Help file's XML will start with two standard declarations:
<?xml version="1.0" encoding="utf-8" ?>
<helpItems xmlns="http://msh" schema="maml">
advertisement
- Getting to grips with Microsoft's IT Health Environment Scanner
- Virtualise your servers
- The changing face of travel gadgets
- Build your own distributed file system
- The bulletproof Dell that costs an arm and a leg
- Microsoft Office 2010 Technical Preview: Q&A
- Lawnmowers, the TyTN II and one odd insurance request
- There'll never be a bulletproof OS
- How far can we trust apps?
- Five nice touches in Outlook 2010
- ATI Radeon HD 5970: 42% more expensive in the UK
- Office 2010 Beta – 32-bit or 64-bit – The Choice is Clear
- Why Britain's watchdogs have fewer teeth than goldfish
- Tabbed documents: how to make Office 2010 great
- Outlook 2010 People Pane – does it spell death to Xobni
- Microsoft Outlook 2010 screenshots
- Co-Authoring in Word 2010 and SharePoint Foundation 2010
- Microsoft Outlook 2010 screenshots: Backstage view
- Flash 10.1: Developing for Desktop and Device
- Microsoft Office 2010 screenshots: Recover unsaved items
- Sky Player shows up in Windows 7
- Tweetlevel reveals most influential Twitterers
- Apple "refuses to repair smokers' Macs"
- Spotify arrives on Symbian
- Chrome OS and Android to "converge over time"
- Microsoft to pay News Corp to stay off Google
- Christmas sales surge knocks out eBay search
- Windows 8 set for 2012 release
- Q&A: Why Conficker was a victim of its own success
- App developers losing faith in Android
advertisement
Printed from www.pcpro.co.uk

