0

我正在为 VB.net 中的 Windows Phone 编写一个程序,该程序下载 XML 文件,读取数据,然后将其输出到文本框。然后,每 3 秒重复一次该过程。我遇到的问题是,当 XML 文件的长度发生变化时,我得到了

'.',十六进制值 0x00,是无效字符

如果文件变小并且

出现意外的文件结尾

如果它变大了。启动时每 3 秒执行一次的代码如下:

    Dim doc = XDocument.Parse(xmlstring)

其中 xmlstring 是一个包含整个 xml 文档内容的字符串。我应该怎么办?

-更新-

这是所有的代码。

    Imports System.Xml.Linq
    Partial Public Class MainPage
Inherits PhoneApplicationPage
Private WithEvents dt As System.Windows.Threading.DispatcherTimer
Private wbcl As New WebClient 'WebClient for downloading XML as string
' Constructor
Public Sub New()
    InitializeComponent()

    ' Set the data context of the listbox control to the sample data
    DataContext = App.ViewModel

End Sub
' Load data for the ViewModel Items
Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Me.Loaded
    If Not App.ViewModel.IsDataLoaded Then
        App.ViewModel.LoadData()
    End If
    AddHandler wbcl.DownloadStringCompleted, AddressOf cl_DownloadStringCompleted 'adds handler for download complete
    wbcl.DownloadStringAsync(New Uri("http://www.copycatmusicracine.com/app/ccatnowplaying.xml?" + "time=" + DateTime.Now.Ticks.ToString)) 'downloads xml as string
    dt = New System.Windows.Threading.DispatcherTimer() 'creates timer
    dt.Interval = New TimeSpan(0, 0, 0, 0, 3000) ' dt_Tick every 3 seconds
    dt.Start() 'starts timer
End Sub
Private Sub dt_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles dt.Tick
    AddHandler wbcl.DownloadStringCompleted, AddressOf cl_DownloadStringCompleted 'adds handler for download complete
    wbcl.DownloadStringAsync(New Uri("http://www.copycatmusicracine.com/app/ccatnowplaying.xml?" + "time=" + DateTime.Now.Ticks.ToString)) 'downloads xml as string
End Sub
Private Sub cl_DownloadStringCompleted(sender As Object, e As System.Net.DownloadStringCompletedEventArgs)
    Dim xmlstring As String = e.Result 'puts XML as string into a new variable
    Dim doc = XDocument.Parse(xmlstring) 'XML is parsed
    If doc.<song>.<title>.Value = "NULL" Then
        'Stuff to happen when song is not playing
    Else
        'Stuff to happen when song is playing
    End If
    End Sub
    End Class

就像我说的,代码第一次工作,但在它启动后,当 XML 文件更改时,它给了我一个错误。

4

0 回答 0