0

I'm getting random errors when trying to load to an XDocument, complaining about invalid characters in my XML.

' ', hexadecimal value 0x1F, is an invalid character. Line 1, position 1.

This happens with one of these 21 locations, but sometimes doesn't. It's the BBC's RSS, so it is strictly managed and encoded XML. When I open the files that I've downloaded and have a problem with, they sometimes look like this inside:

Í–ÛnÛ8†ïôhQ[¤¤ÈvÙÁÆEZ“¢hÒä2 %Ú""‰IÅÍ[õúd;”åSì´nÐ,öƦ43üçð‘P|ò­Èáž+-d9t<—:ÀËD¦¢œ ¯WgÝs2zý*VZº–:bFC'3¦Š™Ïçî<p¥šŸÒüF§uœq‰Q[®‹Wûb鬵}ÔF%€8ÉXYò¼yÀG+墼ƒLñéj?YñÒÍÅ=w'“ÄM¤[ß‘9g&ãŠL9O5á%ñ{Gƒ°L‚”=L¥â ÓÆmtÏ‡ŽæùÔóPñ¡Ãª* 3˜AwX„d™„&ç£ÓÓ1Ü,4 g톀;|È™žÉy¾–ÂðþÁ¦²ˆÉ"´ÝÇÖ1ÚèÈnîmÊ1i\Û°”ëD‰Ê&7 ºXL7Å[m˜*YÀF’e’×vš0_¾2¼¨¸b¦VX™Â\à(qŸ¢©>&›j˼Y9«ÙŒ8ÚW­1‘Õƒ³ÌŒÆËUoÆoáT #t†ÿ’¥6[›ÈXªJªFªšsØÛÃU¡ KSaY~kÁÑ™A\mÅ–)7Lä:&ëÚŒªzòž>ºÊêÐc¸ÀŽùÔÀëGÞ ÂÅ;êQ“¥c'ŠuYnê¸S­ò娵ÁÚ[ªx4zê®OÒd¡‰Hd‰¿e*îEZcÂþíâ/oey›ÛŠo'3âùîLLcbEVŠÏ# ç"5Ù¨ëÅd±Z2Þ´ØZÚeÛ5²Ù¶ûP<n!ÎAiÄ6‚s_˜Àá_ˆRuWk"#èÿø>†7GáïgowÚxpQ'(6¤KÛû—⸱'ã½ÀyÛˆ>Ie2DA#œùCk¿¬ð²ÁÈ¢Ê:p-´˜ˆ\¬ô𫸠eÚÏŠkÝ÷ŠI>Ö…H¯>ýṆ/BßE࣯Ìóz¡wnÁº¬KÍñ<ù^äypzyµï|>yŒœÕ/ ýO;Ç~ )Ë5wjt—v­J—†]z|åõ"J#¿çŽ(ÊEVÏn¿!ÖÜûQ%EiFaèz¡B÷ÈõÃ~/ˆÉ–}Åש½x)ÑÀu“¡Æ¹¬±éìÛ ™pØLøÅô~ à/ó;à„ìBjñ„KY?Iªç†*Ââm³Úëm²ìeU ±4Œpªë6¿Áÿ_o‹ßVË¥ô¿ä÷ÒNv÷zÜ] BŸ8\ðáý€ýßx3¿' ÞLp?À{¨ ¦ÖtÃ~‡Ú—ÚðYÔE¼µþµ­ÖKQ‹X«OòØ~ãÿëWÿÈ U¢<

Is this corrupted? Am I having problems as these weather locations are updated? Here is my code...

Module_XML_Download:

 Public Sub DownloadXML(ByVal URL As String, ByVal SaveAs As String)
    Try
        Dim WebClient As New System.Net.WebClient()

        WebClient.DownloadFile(URL, SaveAs)
        If My.Settings.LogXMLDownloaded = True Then
            Module_Log.WriteLog(Now.ToString & "   XML Downloaded: " & SaveAs)
        End If

    Catch _Exception As Exception

        ' Error
        ' MsgBox("Exception caught in process: {0}", _Exception.ToString())
        If My.Settings.LogXMLDownloadError = True Then
            Module_Log.WriteLog(Now.ToString & "   XML DL Error: " & _Exception.Message)
        End If
    End Try

End Sub

And:

Public Sub DownloadWeather()

    If IsConnectionAvailable() = True Then

        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2657832/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Aberdeen.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2656752/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Aviemore.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2653609/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Barra.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2650752/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Dundee.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2650798/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Dumfries.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2651245/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Dingwall.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2650225/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Edinburgh.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2648579/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Glasgow.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2636239/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Harris.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2646088/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Inverness.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2655051/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Islay.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2636790/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Lewis.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2635754/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Mull.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2641108/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Oban.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2645198/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Orkney.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2640358/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Perth.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2644605/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Shetland.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2640006/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Skye.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2635881/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Thurso.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2643811/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Uist.xml")
        Module_XML_Download.DownloadXML("http://open.live.bbc.co.uk/weather/feeds/en/2635199/3dayforecast.rss", My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Ullapool.xml")

    End If

End Sub

Then an example of one of my loading Subs:

Public Sub WeatherAviemore_Load()
    'weatherDoc = XDocument.Load("http://open.live.bbc.co.uk/weather/feeds/en/2656752/3dayforecast.rss")
    weatherDoc = XDocument.Load(My.Settings.DataMainFolder & "\" & My.Settings.DataWeatherXMLFolder & "\Aviemore.xml")
    PopulateWeatherListBox(weatherDoc, MainForm.WeatherAviemoreListBox, "Aviemore", "An Aghaidh Mhòr")
End Sub
rwmck
  • 25
  • 2
  • 8

2 Answers2

0

The sample you are giving is not xml document and message says that your document is bad from very beginning (Line 1, position 1). A xml document should look like this (wikipedia):

<?xml version="1.0" encoding="UTF-8" ?>
<俄语>данные</俄语>

Edit As I understand it now a file is sometimes downloaded correctly and sometimes doesn't. So the best solution is to accept the fact and attempt to download again. For example

    Dim Success As Boolean = False
    Do
        Try
            weatherDoc = XDocument.Load("http://open.live.bbc.co.uk/weather/feeds/en/2656752/3dayforecast.rss")
            Success = True
        Catch ex As XmlException
            Success = False
        End Try
    Loop Until Success

And maybe ask another question something "Why I am sometimes downloading garbage from open.live.bbc.co.uk/weather?"

IvanH
  • 5,039
  • 14
  • 60
  • 81
-1

I have faced same issue and found the solution, maybe your feed uses GZip compression and you should decompress it.

My posted answer is here, unfortunately I have no knowledge about VB, you should try VB version of C# code.

0x1F XML Error Solution

Have a nice day.

Hamit Enes
  • 122
  • 1
  • 5