Windows Phone 8: Reading XML from Web

淺唱寂寞╮ 提交于 2019-12-23 03:46:17

问题


As part of a project i am working on i am trying to load this XML page (http://weather.yahooapis.com/forecastrss?w=2459115) into my Windows Phone 8 application and save the a number of the values as variables.

I am using Visual Studio 2012 professional and VB.net.

So far i have searched and tried a number of ways just trying to save one variable to start with but i am unable to get it to read.

this is what i have so far

Partial Public Class MainPage
Inherits PhoneApplicationPage
Private XMLHANDLE As WebClient

Dim lang As String

Public Sub New()
    InitializeComponent()

    SupportedOrientations = SupportedPageOrientation.Portrait Or SupportedPageOrientation.Landscape


    Dim XML As String = "http://weather.yahooapis.com/forecastrss?w=2459115"
    XMLHANDLE = New WebClient()
    XMLHANDLE.DownloadStringAsync(New Uri(XML))

    AddHandler XMLHANDLE.DownloadStringCompleted, AddressOf XMLHANDLE_DownloadStringCompleted
    AddHandler XMLHANDLE.DownloadProgressChanged, AddressOf XMLHANDLE_DownloadProgressChanged
End Sub


Private Sub XMLHANDLE_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)

End Sub


Private Sub XMLHANDLE_DownloadStringCompleted(sender As Object, e As DownloadStringCompletedEventArgs)
    If e.[Error] Is Nothing AndAlso Not e.Cancelled Then
        Dim resultElements As XElement = XElement.Parse(e.Result)


        lang = resultElements.Element("language").Value
        Console.WriteLine(lang)
    End If

End Sub

My code keeps Finding a null value exception on this line

lang = resultElements.Element("language").Value

回答1:


Element looks only over direct children of current element, so you can't get language element that simple.

Try that:

lang = resultElements.Element("channel").Element("language").Value

Or using Descendants method:

lang = resultElements.Descendants("language").First().Value


来源:https://stackoverflow.com/questions/15988649/windows-phone-8-reading-xml-from-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!