Grabbing Weather Info in VB.net

Using XML, you can grab weather information via Weather Underground, or almost any other Service.
By simply parsing the XML file, and searching for the Category you need; for example: If you want to find the highest temp for your location (for the day), simply do this:

Code:
 For Each dayInfo As XElement In xInfo...<simpleforecast>
                fcd.ForecastDate_String = dayInfo...<fahrenheit>.Value
            Next

You'll need to Dim the following though:

Code:
Dim retVal As New ForecastData
Dim xInfo = XElement.Load(url)
Dim fcd As New ForecastData
Dim cnd As New ForecastData

We want the Program to look for <simpleforecast> then look in <fahrenheit> and retrieve said value.
In my case, here's the output:

Code:
current: 91F
high: 94F
low: 82F
wind: 4mph
You can also search the XML for high, low, and wind conditions, using the same method.

Here's the full code:

Code:
 Private Function ReturnForecast(ByVal url As String) As ForecastData

        Dim retVal As New ForecastData

        Try
            Dim xInfo = XElement.Load(url)
            Dim fcd As New ForecastData
            Dim cnd As New ForecastData
            For Each dayInfo As XElement In xInfo...<simpleforecast>
                fcd.ForecastDate_String = dayInfo...<fahrenheit>.Value
            Next
            For Each dayInfo As XElement In xInfo...<simpleforecast>
                cnd.ForecastCond_String = dayInfo...<conditions>.Value
            Next

            For Each fcInfo As XElement In xInfo...<forecastday>
                Dim thisDayForecast As New DayForecastData

                If thisDayForecast.ForecastText <> "" Then
                    fcd.DFD.Add(thisDayForecast)
                End If
            Next

            retVal = fcd

        Catch ex As Exception
            retVal = Nothing
        End Try

        Return retVal

    End Function

    Private Sub dev_grab_weather_Tick(sender As Object, e As EventArgs) Handles dev_grab_weather.Tick

        Dim xUrl As String =
            "http://api.wunderground.com/api/fe47f2cc31bf4c8a/forecast10day/q/" + My.Settings.dev_wth_state + "/" + My.Settings.dev_wth_city + ".xml"

        Dim fdInfo As New ForecastData
        fdInfo = ReturnForecast(xUrl)

        If fdInfo IsNot Nothing Then

            Dim sb As New System.Text.StringBuilder

            sb.AppendLine("Current Weather (" + My.Settings.dev_wth_city + ", " + My.Settings.dev_wth_state + "): " & fdInfo.ForecastDate_String + "°")
            sb.AppendLine()
            sb.AppendLine()

            For i As Integer = 0 To fdInfo.DFD.Count - 1
                If i > 0 Then
                    sb.AppendLine()
                    sb.AppendLine("*****")
                    sb.AppendLine()
                End If

                sb.AppendLine("Day: " & fdInfo.DFD(i).Title)
                sb.AppendLine("Forecast: " & fdInfo.DFD(i).ForecastText)
            Next

            If sb.ToString.Length > 0 Then
                lbl_weather.Text = sb.ToString
            End If
        Else
            lbl_weather.Text = "Unable to grab Weather Information!"
        End If

    End Sub

After the Value(s) have been found, they'll be appended to line(s), then displayed upon a label. You can also have it displayed in a Richtextbox, or Messagebox, even a list. In the code above, the XML is retrieved and read every 1000 intervals, when it is being grabbed, the Program may lag (for about 1-2s); then while display the Information. If it fails to retrieve said info, it'll display "Unable to grab Weather Information".

I don't know how exactly to Retrieve weather Information through something such as "The Weather Channel, Google, Yahoo, etc." But, i do think Yahoo uses an XML file as well.

By the way, this code is snipped from Zulas OS, a Desktop Interface (Overlay; a Side Project) for Windows XP+.
If you have any questions, just ask.


The Dr. is out! --It's kinda hard to explain in words though :P

Comments

There are no comments to display.

Blog entry information

Author
Dr.Hacknik
Views
401
Last update

More entries in Personal Blogs

More entries from Dr.Hacknik

  • Thanks
    Well for as long as I've been here (four years now) I must thank the...
  • My Average Day [First Blog]
    Well, i recently hacked my Wii U, and i'm quite happy about it! I've...
General chit-chat
Help Users
    ZeroT21 @ ZeroT21: brand name means shit