Windows Phone 8:从 Web 读取 XML [英] Windows Phone 8: Reading XML from Web

查看:27
本文介绍了Windows Phone 8:从 Web 读取 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我使用的是 Visual Studio 2012 专业版和 VB.net.

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.

这是我目前所拥有的

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

推荐答案

Element 只查看当前元素的直接子元素,所以你不能得到 language 元素很简单.

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

试试:

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

或者使用Descendants方法:

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

这篇关于Windows Phone 8:从 Web 读取 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆