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:
- Open PowerShell ISE as an administrator.
- 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] $xml = Get-Content $vancouverXML #get number of entries $xml.climatedata.stationdata.Count #store max temps in array $maxtemp = $xml.climatedata.stationdata | Foreach...