#PSTip Decoding an HTML-encoded string

In my previous tip, I showed some of my favorite methods to convert date time string to the local time. In the same context of RSS feeds, I sometimes find the description text in the RSS feed is HTML-encoded. One example is the Azure service update feed.

[Reflection.Assembly]::LoadWithPartialName('System.Web')
$feed = 'http://azure.microsoft.com/en-us/updates/feed/'
$xml = Invoke-RestMethod -Uri $feed
$xml[1].description

#output from the above snippet
<p>We&#8217;ve created a new Web Deploy endpoint that lives as a site extension on each customer&#8217;s site. </p>

When I want a readable text, this is not very helpful. So, here is one method I use to decode this string into something that is meaningful to me.

$xml | Select Title, @{Name='PublicationDate';Expression={[DateTime]$_.PubDate}}, @{Name='Description';Expression={([System.Web.HttpUtility]::HtmlDecode($_.Description)) -replace "<.*?>",''}}

html

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts