Parsing XML

In this recipe, we will parse sample XML using PowerShell.

Getting ready

In this recipe, we will use Vancouver's 2012 daily weather data, which can be downloaded from http://www.climate.weatheroffice.gc.ca/climateData/dailydata_e.html?Prov=BC&StationID=889&Year=2012&Month=4&Day=30&timeframe=2.

How to do it...

Let's take a look at how we can parse XML files:

  1. Open PowerShell ISE as an administrator.
  2. Add the following script and run it:
    $vancouverXML = "C:\XML Files\eng-daily-01012012-12312012.xml" $uri = "http://climate.weather.gc.ca/climateData/bulkdata_e.html?format=xml&stationID=889&Year=2012&Month=4&Day=1&timeframe=2&submit=Download+Data" #download Vancouver weather into XML file Invoke-WebRequest -Uri $uri -OutFile $vancouverXML [xml] ...

Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.