Invoke-restmethod encoding problem

Hi Guys,

When i’m running the following Powershell code I get the wrong encoding. I need it to be in utf-8 but it’s not

[pre]$from = [xml](Invoke-RestMethod -Method Get -Uri (“http://xmltv.xmltv.se/tv3.dk_2020-08-19.xml”) -ContentType “text/xml”)

$from.tv.programme.desc [/pre]

I get these weird characters:

“De sørgende enker”

“NÃ¥r Luksusfælden til efterÃ¥ret vender tilbage”

In Denmark we use letters ÆØÅ :slight_smile:

Do you guys know what i’m doing wrong?

 

You could test by adding charset=utf-8 to your -ContentType value.

[quote quote=251123]You could test by adding charset=utf-8 to your -ContentType value.

[/quote]
It didn’t work I tried the following:

$from = [xml](invoke-restmethod -Method get -uri http://xmltv.xmltv.se/tv3.dk_2020-08-19.xml -ContentType "application/xml; Charset=utf-8")

$from.tv.programme.desc
One funny thing. When I ran the following code I didn't get the weird characters:
Invoke-RestMethod -Method Get -Uri ("http://xmltv.xmltv.se/tv3.dk_2020-08-19.xml")  -ContentType "application/xml" -OutFile C:\users\[my user name]\OneDrive\Desktop\kanaler.xml
$XMLFile = Get-Content -Path C:\users\dvide\onedrive\desktop\kanaler.xml -Encoding UTF8
$XMLFile.tv.programme.desc
So I think there is something wrong in my script somewhere. Here is my "epg grabber script":
[DateTime]$StartDate = Get-Date [int]$SevenDays = 7 [DateTime]$EndDate = ($StartDate).AddDays($SevenDays)

$Channels = @{

‘animalplanet.discovery.dk’ = @{ ChannelName = ‘Animal Planet’ ChannelNumber = ‘28’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Animal_Planet.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Animal_Planet.png’ } ‘canal9.dk’ = @{ ChannelName = ‘Canal 9’ ChannelNumber = 9 ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Canal_9.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Canal_9.png’ } ‘ultrahd.viasat.dk’ = @{ ChannelName = ‘Viasat Ultra HD’ ChannelNumber = ‘38’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_Ultra_HD.png’ ProgrammeIcon = ‘’ } ‘series.viasat.dk’ = @{ ChannelName = ‘Viasat Series’ ChannelNumber = ‘24’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_Series.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Viasat_Series.png’ } ‘nature.viasat.dk’ = @{ ChannelName = ‘Viasat Nature’ ChannelNumber = ‘27’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_Nature.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Viasat_Nature.png’ } ‘history.viasat.dk’ = @{ ChannelName = ‘Viasat History’ ChannelNumber = ‘25’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_History.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Viasat_History.png’ }#> ‘film.viasat.dk’ = @{ ChannelName = ‘Viasat Film Premiere’ ChannelNumber = ‘20’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_Film_Premiere.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Viasat_Film_Premiere.png’ } ‘hits.film.viasat.dk’ = @{ ChannelName = ‘Viasat Film Hits’ ChannelNumber = ‘22’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_Film_Hits.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Viasat_Film_Hits.png’ } ‘family.film.viasat.dk’ = @{ ChannelName = ‘Viasat Film Family’ ChannelNumber = ‘23’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_Film_Family.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Viasat_Film_Family.png’ } ‘action.film.viasat.dk’ = @{ ChannelName = ‘Viasat Film Action’ ChannelNumber = ‘21’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_Film_Action.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Viasat_Film_Action.png’ } ‘explorer.viasat.dk’ = @{ ChannelName = ‘Viasat Explore’ ChannelNumber = ‘26’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_Explore.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Viasat_Explore.png’ } ‘classic.vh1.se’ = @{ ChannelName = ‘VH1 Classic’ ChannelNumber = ‘34’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/VH1%20Classic.png’ ProgrammeIcon = ‘’ } ‘vh1.mtv.dk’ = @{ ChannelName = ‘VH1’ ChannelNumber = ‘33’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/VH1.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/VH1.png’ } <#‘2030.honeybee.it’ = @{ ChannelName = ‘TV4 Sverige’ ChannelNumber = ‘45’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV4_Sverige.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV4_Sverige.png’ }#> ‘tv3plus.dk’ = @{ ChannelName = ‘TV3+’ ChannelNumber = ‘4’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV3%2B.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV3%2B.png’ } ‘one.tv3sport.dk’ = @{ ChannelName = ‘TV3 Sport’ ChannelNumber = ‘13’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV3_Sport.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV3_Sport.png’ } ‘puls.tv3.dk’ = @{ ChannelName = ‘TV3 Puls’ ChannelNumber = ‘12’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV3_Puls.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV3_Puls.png’ } ‘max.tv3.dk’ = @{ ChannelName = ‘TV3 Max’ ChannelNumber = ‘11’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV3_Max.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV3_Max.png’ } ‘tv3.dk’ = @{ ChannelName = ‘TV3’ ChannelNumber = ‘3’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV3.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV3.png’ } ‘zulu.tv2.dk’ = @{ ChannelName = ‘TV2 Zulu’ ChannelNumber = ‘7’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV2_Zulu.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV2_Zulu.png’ } ‘sport.tv2.dk’ = @{ ChannelName = ‘TV2 Sport’ ChannelNumber = ‘14’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV2_Sport.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV2_Sport.png’ } ‘news.tv2.dk’ = @{ ChannelName = ‘TV2 News’ ChannelNumber = ‘10’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV2_News.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV2_News.png’ } ‘tv2.dk’ = @{ ChannelName = ‘TV2’ ChannelNumber = ‘2’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV2.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV2.png’ } <#‘2451.honeybee.it’ = @{ ChannelName = ‘TV2 Norge’ ChannelNumber = ‘47’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV2_Norge.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV2_Norge.png’ }#> ‘fri.tv2.dk’ = @{ ChannelName = ‘TV2 Fri’ ChannelNumber = ‘42’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV2_Fri.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV2_Fri.png’ } ‘charlie.tv2.dk’ = @{ ChannelName = ‘TV2 Charlie’ ChannelNumber = ‘39’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TV2_Charlie.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TV2_Charlie.png’ } ‘tlc.discovery.dk’ = @{ ChannelName = ‘TLC Danmark’ ChannelNumber = ‘40’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/TLC.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/TLC.png’ } <#‘1969.honeybee.it’ = @{ ChannelName = ‘SVT1’ ChannelNumber = ‘44’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/SVT1.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/SVT1.png’ } ‘2478.honeybee.it’ = @{ ChannelName = ‘NRK1’ ChannelNumber = ‘46’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/NRK1.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/NRK1.png’ }#> ‘natgeo.dk’ = @{ ChannelName = ‘National Geographic’ ChannelNumber = ‘29’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/National_Geographic_Channel.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/National_Geographic_Channel.png’ } ‘mtv.dk’ = @{ ChannelName = ‘MTV Danmark’ ChannelNumber = ‘41’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/MTV.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/MTV.png’ } ‘kanal5.dk’ = @{ ChannelName = ‘Kanal 5’ ChannelNumber = ‘5’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Kanal_5.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Kanal_5.png’ } ‘kanal4.dk’ = @{ ChannelName = ‘Kanal 4’ ChannelNumber = ‘30’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Kanal_4.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Kanal_4.png’ } ‘ft.dk’ = @{ ChannelName = ‘Folketinget’ ChannelNumber = ‘48’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Folketinget.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Folketinget.png’ } ‘eurosport.sbstv.dk’ = @{ ChannelName = ‘Eurosport 2’ ChannelNumber = ‘16’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Eurosport_2.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Eurosport_2.png’ } ‘eurosport.dk’ = @{ ChannelName = ‘Eurosport 1’ ChannelNumber = ‘15’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Eurosport_1.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Eurosport_1.png’ } ‘esports.viasat.dk’ = @{ ChannelName = ‘eSportsTV’ ChannelNumber = ‘49’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/esportstv.png’ } ‘dr2.dr.dk’ = @{ ChannelName = ‘DR2’ ChannelNumber = ‘8’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/DR2.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/DR2.png’ } ‘dr1.dr.dk’ = @{ ChannelName = ‘DR1’ ChannelNumber = ‘1’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/DR1.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/DR1.png’ } ‘6-eren.dk’ = @{ ChannelName = ‘6eren’ ChannelNumber = ‘6’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/6eren.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/6eren.png’ } ‘golf.viasat.dk’ = @{ ChannelName = ‘Viasat Golf’ ChannelNumber = ‘33’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Viasat_Golf.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Viasat_Golf.png’ } ‘investigation.discovery.dk’ = @{ ChannelName = ‘Investigation Discovery’ ChannelNumber = ‘19’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Investigation_Discovery.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Investigation_Discovery.png’ } ‘discovery.dk’ = @{ ChannelName = ‘Discovery Channel’ ChannelNumber = ‘18’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/Discovery_Channel.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/Discovery_Channel.png’ } ‘dk4.dk’ = @{ ChannelName = ‘DK4’ ChannelNumber = ‘32’ ChannelIcon = ‘https://raw.githubusercontent.com/dvidebaek/DK_Channel_Logos/master/DK4.png’ ProgrammeIcon = ‘https://raw.githubusercontent.com/dvidebaek/Plex_Thumbs/master/DK4.png’ }

}

[XML]$EPG = New-Object System.Xml.XmlDocument

$XMLDeclaration = $EPG.CreateXmlDeclaration(“1.0”,“UTF-8”,$null)

$EPG.AppendChild($XMLDeclaration)

$XMLRootNode = $EPG.CreateNode(“element”,“tv”,$null)

foreach ($Channel in $Channels.Keys) {

$ChannelName = ($Channels[$Channel]).ChannelName $ChannelIcon = ($Channels[$Channel]).ChannelIcon $ChannelNumber = ($Channels[$Channel]).ChannelNumber$XMLChannel = $EPG.CreateNode("element","channel",$null) $XMLChannel.SetAttribute("id","$ChannelName") $XMLDisplayName = $EPG.CreateNode("element","display-name",$null) $XMLDisplayName.SetAttribute("lang","da") $XMLDisplayName.InnerText = "$ChannelName" $XMLIcon = $EPG.CreateNode("element","icon",$null) $XMLIcon.SetAttribute("src","$ChannelIcon") $XMLLcn = $EPG.CreateNode("element","lcn",$null) $XMLLcn.InnerText = "$ChannelNumber"

$XMLChannel.AppendChild($XMLDisplayName) $XMLChannel.AppendChild($XMLIcon) $XMLChannel.AppendChild($XMLLcn) $XMLRootNode.AppendChild($XMLChannel)

}

$EPG.AppendChild($XMLRootNode)

for ($i = $StartDate; $i -le $EndDate; $i=$i.AddDays(1)) { $Dates = $i.ToString(‘yyyy-MM-dd’)

    foreach ($Programmes in $Channels.Keys) {

        $From = [xml](Invoke-RestMethod -Method Get -Uri ("http://xmltv.xmltv.se/$($Programmes)_$($Dates).xml") -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome -ContentType "application/xml")

            foreach ($node in $From.tv.programme) {

                $XMLProgrammeChannel = ($Channels[$node.channel]).ChannelName $node.SetAttribute("channel","$XMLProgrammeChannel") $EPG.tv.AppendChild($EPG.ImportNode($node, $true)) } } }

$EPG.Save(‘C:\users\dvide\OneDrive\Desktop\testxml.xml’)

I now believe it's not the invoke-restmethod that generates the weird characters. Maybe it's $EPG.Save('C:\users\dvide\OneDrive\Desktop\testxml.xml')
I'm lost hope somebody can help me

Invoke-RestMethod is just returning at payload from an API. This can be tested by using curl or postman and looking at the XML that is returned. You can save the content directly with Out-File, but it appears you should be concentrating on where exactly the characters appears starting at the source. Another option:

https://stackoverflow.com/questions/30265973/how-can-i-force-or-specify-encoding-type-using-xml-save

[quote quote=251690]Invoke-RestMethod is just returning at payload from an API. This can be tested by using curl or postman and looking at the XML that is returned. You can save the content directly with Out-File, but it appears you should be concentrating on where exactly the characters appears starting at the source. Another option:

https://stackoverflow.com/questions/30265973/how-can-i-force-or-specify-encoding-type-using-xml-save

[/quote]

Used -outfile and loaded the xml. That way it worked. The link advised I couldn’t get to work. I’m Running this on PowerShell 7.0.3 by the way.

 

[pre] Invoke-RestMethod -Method Get -Uri “http://xmltv.xmltv.se/$($Programmes)_$($Dates).xml” -ContentType “application/xml” -OutFile “C:\users\dvide\Desktop$($Programmes)-$($Dates).xml”

[xml]$LoadXML = Get-Content -Encoding utf8NoBOM -Path "C:\users\dvide\Desktop\$($Programmes)-$($Dates).xml" [/pre]
Don't know whats going on in PowerShell... Maybe it's a bug?