Skip to navigation

PCPro-Computing in the Real World Printed from www.pcpro.co.uk

Register to receive our regular email newsletter at http://www.pcpro.co.uk/registration.

The newsletter contains links to our latest PC news, product reviews, features and how-to guides, plus special offers and competitions.

Real World Computing

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">

1 2 3 4
Be the first to comment this article

You need to Login or Register to comment.

(optional)

advertisement

Latest Real World Computing
Latest Blog Posts Subscribe to our RSS Feeds
Latest News Stories Subscribe to our RSS Feeds
Latest Reviews Subscribe to our RSS Feeds

advertisement

Sponsored Links
 
SEARCH
SIGN UP

Your email:

Your password:

remember me

advertisement


Hitwise Top 10 Website 2008