1

作为我正在处理的项目的一部分,我正在尝试将此 XML 页面 ( http://weather.yahooapis.com/forecastrss?w=2459115 ) 加载到我的 Windows Phone 8 应用程序中,并将一些值保存为变量.

我正在使用 Visual Studio 2012 专业版和 VB.net。

到目前为止,我已经搜索并尝试了多种方法,只是试图保存一个变量以开始,但我无法读取它。

这就是我到目前为止所拥有的

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

我的代码一直在此行上查找空值异常

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

1 回答 1

1

Element仅查看当前元素的直接子元素,因此您无法获得language那么简单的元素。

试试看:

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

或使用Descendants方法:

lang = resultElements.Descendants("language").First().Value
于 2013-04-13T14:09:06.093 回答