[英]Get value from XML file from web
从Web的xml获取日出时间和日落时间。 这是xml的链接:
http://www.yr.no/sted/Norge/Oslo/Oslo/Blindern/varsel.xml
我尝试使用以下网站中的信息: http : //www.jeffblankenburg.com/2010/10/25/31-days-of-windows-phone-day-25-talking-to-external-apis/
我正在尝试通过此操作将他的信息更改为我的信息,但仅得到NullReferenceException:
private void GoButton_Click(object sender, RoutedEventArgs e)
{
if (NetworkInterface.GetIsNetworkAvailable())
{
WebClient twitter = new WebClient();
twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
twitter.DownloadStringAsync(new Uri("http://www.yr.no/sted/Norge/Oslo/Oslo/Blindern/varsel.xml"));
}
}
void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null) return;
XElement xmlTweets = XElement.Parse(e.Result);
string name = xmlTweets.Element("weatherdata").Element("location").Element("country").Value;
TwitterName.Text = name;
}
这是来自网络xml文件的剪辑。 它很大,但是我只需要时间晒太阳和时间升起..请帮助。
<weatherdata>
<location>
<name>Blindern</name>
<type>Byområde</type>
<country>Norge</country>
<timezone id="Europe/Oslo" utcoffsetMinutes="120"/>
<location altitude="90" latitude="59.9406284402542" longitude="10.7230684724138" geobase="ssr" geobaseid="73738"/>
</location>
<credit>...</credit>
<links>...</links>
<meta>...</meta>
<sun rise="2012-05-19T04:30:13" set="2012-05-19T21:58:34"/>
<forecast>...</forecast>
<observations>...</observations>
</weatherdata>
该元素已经在<weatherdata>
节点上,因此您无需再次查询。
string name = xmlTweets.Element("location").Element("country").Value;
TwitterName.Text = name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.