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">
From around the web
advertisement
- Why virtualisation hasn't slowed the growth of data
- How to make Google AdWords work for your business
- The curse of sloppily written software
- Paying for your crimes with Bitcoin
- Behind the scenes: tech support for Formula 1
- The security risk of fat fingers
- Why Windows Phone 7 isn't quite ready for business
- When will Microsoft stop fiddling with Windows 8?
- Flash down the pan?
- Metro Style apps vs desktop applications
- Chrome's shine getting lost in translation
- BytePac: the cardboard hard disk enclosure
- How tech loosens our grip on reality
- Hokum watch: Safer Internet Day
- Why I'm deleting Adobe from my PC
- Prepare to be patronised: it's Safer Internet Day
- Dear Sony, Samsung and every other tech company in the world: stop trying to be Apple
- Will Apple's Final Cut Pro X update placate the pros?
- Smartr Contacts for iPhone review
- Switching to Office 365's Outlook Web App
- VeriSign slammed for security breach cover-up
- SAP willing to share HANA with Oracle
- Why using a tablet could harm your health
- New RIM boss: no need for drastic change
- RIM founders fall on their swords
- Slow economy helps boost Red Hat revenue by 23%
- Google+ pages get multiple admins
- One in five companies lack card industry compliance
- Oil industry warns hacking attacks could kill
- British workers fear email monitoring
advertisement
