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:
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
- 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
- Microsoft Word 2010 screenshots: Text Effects
- Microsoft Word 2010: inserting screenshots
- Q&A: Why Conficker was a victim of its own success
- App developers losing faith in Android
- Biz Stone: Murdoch's Google veto will "fail fast"
- Google adds automatic captions to YouTube
- China ramps up cyber spying
- Mozilla maintains dependence on Google
- Windows 7 flying off the shelves
- Google Chrome OS: full details unveiled
- AOL slashes 2,500 jobs
- YouTube begins streaming full-length shows
advertisement
Printed from www.pcpro.co.uk

