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.

eworder.xml).

The screenshot above shows the result of cutting and pasting this script into PowerShell's console. One of the useful things about doing this is that you can examine the individual elements that were used to update the document.

Typing $ol into PowerShell's console displays the new order line added to the order and, as I showed in last month's column, the sub-elements of this order line are directly addressable: $ol.ItemID gives the part number, while $ol.NumberOrdered gives the number of items ordered, as follows:

PSH [D:\foo]: $ol.itemid

876543

PSH [D:\foo]: $ol.numberordered

21

PSH [D:\foo]:

PowerShell Community Extensions

One of the most powerful aspects of PowerShell is the way the community can add to the components shipped by Microsoft. You can write your own extensions or utilise those already created by some of the really smart PowerShell gurus. You can download these from CodePlex at http://tinyurl.com/ywk28h.

One of the nicer cmdlets in the Community Extensions is the Format-XML cmdlet. You can use this cmdlet to display the XML we built above, as follows:

PSH [D:\foo]: $ol.get_outerxml()

<OrderLine><ItemID>876543</ItemID><ItemDescription>Invisible Widgets</ItemDescription><NumberOrdered>21</NumberOrdered><Price>321

.12</Price></OrderLine>

PSH [D:\foo]:

PSH [D:\foo]: format-xml -input ($ol.get_outerxml())

<OrderLine>

<ItemID>876543</ItemID>

<ItemDescription>Invisible Widgets</ItemDescription>

<NumberOrdered>21</NumberOrdered>

<Price>321.12</Price>

</OrderLine>

PSH [D:\foo]:

PSH [D:\foo]: $ol.get_innerxml()

<ItemID>876543</ItemID><ItemDescription>Invisible Widgets</ItemDescription><NumberOrdered>21</NumberOrdered><Price>321.12</Price>

PSH [D:\foo]:

PSH [D:\foo]: format-xml -input ($ol.get_innerxml())

<ItemID>876543</ItemID>

<ItemDescription>Invisible Widgets</ItemDescription>

<NumberOrdered>21</NumberOrdered>

<Price>321.12</Price>

PSH [D:\foo]:

As you can see, typing $ol.get_outerXML() causes PowerShell to display the full XML for the newly added order line, while $ol.get_innerXML() gets the inner XML, and by also applying the Format-XML cmdlet, you can examine the full outer and inner XML for this newly created order line in properly indented form.

Another useful cmdlet in the Community Extensions package is the Test-XML cmdlet, which tests an XML file for validity and can optionally validate a document against a named XML schema. Test-XML performs a basic set of tests, returning a simple true if the XML is well formed (and optionally validating against the schema), or false if not. You can use the Verbose switch when calling Test-XML to report on any errors found.

As noted on the Community Extensions site, this Test-XML cmdlet is still under development, but it certainly catches a number of errors already.

XML Readers in PowerShell

In the examples I've shown so far, loading the XML file using the DOM (Document Object Model) method has been straightforward, and makes for simple coding. However, there's a downside to doing things the DOM way - it isn't particularly efficient. With DOM, PowerShell has to first load the whole file into memory and then create an XML document representing the entire file, complete with the overhead of the XML DOM format.

A much more efficient method of processing XML documents is to use the XML Reader class, which streams through the document one element at a time instead of loading the whole thing into memory in one go. Here's a simple example using the class called loop-xml:

1 2 3 4
Be the first to comment this article

You need to Login or Register to comment.

(optional)

advertisement

Most Commented Real World Articles
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