In the previous tip we explained how you can retrieve XML data from webpages using either Invoke-WebRequest or Invoke-RestMethod. For XML data, there is also another approach which uses the built-in methods found in the XML object itself.

This was the approach with Invoke-RestMethod:

$url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
(Invoke-RestMethod -Uri $url -UseBasicParsing).Envelope.Cube.Cube.Cube

As an alternative, try this:

$url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
$xml = New-Object -TypeName XML
$xml.Load($url)
$xml.Envelope.Cube.Cube.Cube

It is much faster; however, it does not provide you with the multitude of parameters available in Invoke-RestMethod to handle proxy servers and credentials.

Twitter This Tip! ReTweet this Tip!

Anonymous