随机XML异常 - '',十六进制值0x1F,是无效字符

时间:2013-05-09 16:33:21

标签: xml vb.net linq-to-xml hex

我在尝试加载到XDocument时遇到随机错误,抱怨我的XML中存在无效字符。

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

这种情况发生在这21个地点中的一个,但有时却没有。它是BBC的RSS,因此它是严格管理和编码的XML。当我打开我下载并遇到问题的文件时,它们有时看起来像这样:

I-Û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

这是否已损坏?这些天气位置是否更新,我有问题吗?这是我的代码......

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

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

然后我的一个加载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

2 个答案:

答案 0 :(得分:0)

您提供的示例不是xml文档,并且消息表明您的文档从一开始就很糟糕(Line 1, position 1)。 xml文档应如下所示(wikipedia):

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

修改 据我所知,现在有时会正确下载文件,有时却没有。因此,最好的解决方案是接受事实并尝试再次下载。例如

    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

也许还会问另一个问题“为什么我有时会从open.live.bbc.co.uk/weather下载垃圾?”

答案 1 :(得分:-1)

我也遇到了同样的问题并找到了解决方案,也许您的Feed使用GZip压缩,您应该将其解压缩。

我的发布答案在这里,很遗憾,我对VB不了解,您应该尝试VB版本的C#代码。

0x1F XML Error Solution

今天愉快。