SilverLight读取XML文件

首先第一步用ScriptOjbect对象确定要读取的文件的位置,代码如下:

Public Shared Function GetUrl() As String
        Dim location As ScriptObject = TryCast(HtmlPage.Window.GetProperty("location"), ScriptObject)
        Dim r As Object = location.GetProperty("href")
        Dim url As String = r.ToString().Substring(0, r.ToString().LastIndexOf("/"))
        Return url
End Function

其次用WebClient对象以及uri对象下载xml文件,代码如下:

Private Sub Init()
        Dim uri As Uri = New Uri(GetUrl() + "/config.xml", UriKind.Absolute)
        Dim client As WebClient = New WebClient()
        AddHandler client.OpenReadCompleted, AddressOf OpenReadCompleted
        client.OpenReadAsync(uri)
End Sub

最后用XMLReader对象对下载的XML文件进行加工处理,代码如下:

Private Sub OpenReadCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs)
        Dim stream As Stream = e.Result
        Using reader As XmlReader = XmlReader.Create(stream)
            reader.ReadToFollowing("TestData")
            Dim testData As String = reader.ReadElementContentAsString()
            reader.ReadToNextSibling("TestData2")
            Dim testData2 As String = reader.ReadElementContentAsString()
        End Using
End Sub

也许在Asp.net中微软的WebConfigurationManager对象以及ConfigurationManager对象也是这样工作的吧!哈哈,只是猜测!

你可能感兴趣的:(xml,object,String,function,Stream,silverlight)