简体   繁体   中英

Get value from XML file from web

Get sunrise time and sun set times from xml from web. This is the link to the xml:

http://www.yr.no/sted/Norge/Oslo/Oslo/Blindern/varsel.xml

I have tried to use the information in the: http://www.jeffblankenburg.com/2010/10/25/31-days-of-windows-phone-day-25-talking-to-external-apis/

I am trying to change his information to my information by doing this but is only getting 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;

}

Here is a clip from the xml file from the net. It is quite big, but i only need the time for sun set and the time for sun rise.. Please help.

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

The element is already at the <weatherdata> node, so you don't need to query for it again.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM