none
PowerShell - XML from WebSite RRS feed

  • Question

  • Hey Scripting Guys,

    I have a script that gets XML content as [xml] and then I go through the nodes to get information about the nodes contents. This all works as expected. I then want to be able to host this XML file on a website and get the content from there, rather than have multiple copies on multile environments.

    So I have hosted the file and I run the following script to test.

    $xmlURL = "http://192.168.1.254/Website/File.xml"
    $xmlSchedule = [xml](New-Object System.Net.WebClient).downloadstring($xmlURL)


    This outputs 

    Cannot convert value "ÿþ< ? x m l   v e r s i o n = " 1 . 0 "   e n c o d i n g = " u t f - 8 " ? >

    "XML"

     " to type "System.Xml.XmlDocument". Error: "Data at the root level is invalid. Line 1, position 1."
    At line:1 char:69
    + $xmlSchedule = [xml](New-Object System.Net.WebClient).downloadstring <<<< ($xmlURL)
        + CategoryInfo          : NotSpecified: (:) [], RuntimeException
        + FullyQualifiedErrorId : RuntimeException

    Any help would be awesome.. 

    Thanks in advance.

    Monday, May 16, 2016 1:59 PM

Answers

  • Use Download file and not download string.

    (New-Object System.Net.WebClient).DownloadFile($xmlURL,'c:\temp\file.xml')
    $xmlSchedule = [xml](Get-Content 'c:\temp\file.xml')

    this will create the correct file encoding.


    \_(ツ)_/

    Monday, May 16, 2016 2:42 PM

All replies

  • I see a strange character "ÿþ .

    This could be due to BOM.

     http://www.ipreferjim.com/2014/09/data-at-the-root-level-is-invalid-line-1-position-1/

    Monday, May 16, 2016 2:32 PM
  • Use Download file and not download string.

    (New-Object System.Net.WebClient).DownloadFile($xmlURL,'c:\temp\file.xml')
    $xmlSchedule = [xml](Get-Content 'c:\temp\file.xml')

    this will create the correct file encoding.


    \_(ツ)_/

    Monday, May 16, 2016 2:42 PM
  • BOM is something new i learnt today. I was just curious to see if it is indeed the same.

    Would you please run this and confirm ?

    $bomutf8=[system.text.Encoding]::UTF8.GetPreamble()
    if(
    ($a=(New-Object System.Net.WebClient).Downloadstring($xmlURL)).startsWith($bomutf8)
    )
    {
    	[xml]$a=$a.remove(0,$bomutf8.length)
    }
    else
    {echo NoBOM}

    Many thanks.

    jrv's suggestion looks appropriate. 


    Monday, May 16, 2016 3:03 PM
  • The BOM for the string is:

    [int]'ÿþ'[0]
    FF
    [int]'ÿþ'[1]
    FE

    Which is FFFE which is UTF-16

    This is because the browser talks I  UTF-16.  This is why I said to download file as the download should create a file that matches the header which stats UTF-8.  The file should download in its native format and not get altered by the browser.


    \_(ツ)_/

    Monday, May 16, 2016 3:14 PM
  • Thanks jrv. This is useful
    Monday, May 16, 2016 3:41 PM
  • Thanks jrv. This is useful

    So is yours.

    \_(ツ)_/

    Monday, May 16, 2016 4:01 PM
  • i have xml file with utf-8.

    try this 

    $wb=New-Object System.Net.WebClient -Property @{Encoding = [System.Text.Encoding]::UTF8}
    $xmlURL = 'http://localhost/lider1.xml'
    $xmlSchedule=[xml]($wb.DownloadString($xmlURL))

    Wednesday, January 23, 2019 12:40 PM