2012-05-19 31 views
2

从xml获取日出时间和日落时间。 这是链接到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; 

} 

这是来自net的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> 

回答

1

的元素已经在<weatherdata>节点,所以你不需要再次查询它。

string name = xmlTweets.Element("location").Element("country").Value; 
TwitterName.Text = name; 
+0

哦,我的上帝,谢谢:)我怎样才能得到 Megaoctane

+0

使用'Attribute'方法而不是'Element'去到'rise'属性:'xmlTweets.Element(“sun”)。属性(“rise”)。值' – carlosfigueira

+0

这样做。非常感谢你:) – Megaoctane