Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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] $xml = Get-Content $vancouverXML
    
    #get number of entries
    $xml.climatedata.stationdata.Count
    
    #store max temps in array
    $maxtemp = $xml.climatedata.stationdata | 
    Foreach-Object 
    { 
       [int]$_.maxtemp....