PowerShell Tricks – Basic XML Parsing

Programming Tips and Tricks Tips-And-Tricks

XML is everywhere, and you can’t (and most probably don’t want to) get rid of it. Pulling in data from heterogeneous technologies in a single well defined and widely accepted data format, that is XML, has made a developer’s life far comfortable than ever.

PowerShell makes handling XML quite simple. It converts XML elements to properties of .NET objects without the need to write any complex code. So all you need is PowerShell installed on your machine and you’re ready to go!

Assuming you have a sample xml file like below and here are some simple yet useful PowerShell examples to show how PowerShell maps the XML elements and attributes to object properties.

<players>
	<player id="001">
		<name>Allan Border</name>
		<country>Australia</country>
		<game>Cricket</game>		
		<rating>10</rating>		
	</player>
	<player id="002">
		<name>Bob Willis</name>
		<country>England</country>
		<game>Cricket</game>		
		<rating>9</rating>		
	</player>
	<player id="003">
		<name>Richard Hadlee</name>
		<country>New Zealand</country>
		<game>Cricket</game>		
		<rating>10</rating>		
	</player>
	<player id="004">
		<name>Michael Peter</name>
		<country>West Germany</country>
		<game>Hockey</game>
		<rating>8</rating>		
	</player>
	<player id="005">
		<name>Jahangir Khan</name>
		<country>Pakistan</country>
		<game>Squash</game>
		<rating>10</rating>		
	</player>
	<player id="006">
		<name>Jimmy Connors</name>
		<country>America</country>
		<game>Tennis</game>
		<rating>9</rating>		
	</player>
</players>
$xml = [xml](get-content F:\Per\PowerShell\sample.xml)
$xml

The square brackets ‘[xml]’ just before the get-content cmdlet is type-casting the text returned from get-content sample.xml to an XmlDocument object. Once you have an XmlDocument object, PowerShell’s built-in support for XML kicks in to let you fiddle with the XML nodes in the XML document.

$xml.Players

prints the name properties of each player like this.

{Allan Border, Bob Willis, Richard Hadlee, Michael Peter…}

$xml.Players.Player

prints all player nodes with all of their respective properties.

$xml.Players.Player[2].name
$xml.Players.Player[3].country

The above two parsers are quite intuitive. They print out the name and the country of the player specified by its index value.

$xml.SelectNodes("/players/player")
$xml.SelectNodes("/players/player/country")

Above is a variation of getting inner nodes using the XPath query syntax, e.g, SelectNodes(). The output will be something like;

Australia
England
New Zealand
West Germany
Pakistan
America

$xml.players.player
$xml.players.player | where { [int]$_.rating -gt 8 }

Provide output to a where clause and filter out the players based on their rating.

$xml.players.player | foreach { $_.name + " == " + $_.rating + "  == " + $_.game  + "  == " + $_.country}

and the last but not the least, you can render the xml content your own way. The output of the above cmdlet will be:

Allan Border == 10 == Cricket == Australia
Bob Willis == 9 == Cricket == England
Richard Hadlee == 10 == Cricket == New Zealand
Michael Peter == 8 == Hockey == West Germany
Jahangir Khan == 10 == Squash == Pakistan
Jimmy Connors == 9 == Tennis == America

Above we have outlines only a handful of examples to explore XML document content using the power of Windows PowerShell. You are encouraged to poke around more to foster your knowledge in this regards.

If you’re curious to learn more about Windows PowerShell techniques, following articles will prove to be more helpful:

PowerShell Technique – Get RSS Feed For Weather Update
Windows PowerShell – Basic Constructs
Windows PowerShell Technique: Obtain Network Data
Windows PowerShell Technique: Get CPU Information
Windows PowerShell Technique: Get Hard Drive Information

1 thought on “PowerShell Tricks – Basic XML Parsing

  1. Hello mates, its impressive piece of writing on the topic of tutoringand fully defined, keep it up all the time.

Leave a Reply

Your email address will not be published. Required fields are marked *