问题
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