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