Weather Alerts Refusing to Display In List Box

Austin Althouse 26 Reputation points
2020-12-28T20:20:05.693+00:00

Hello,

I have a weather application that uses a list box and a ticker to display weather information. However, the list box refuses to display any bit of information while the ticker remains functioning.

Here is my code:

 Private Sub NOAA_Alerts()

        Using WB As New WebClient
            Try
                WB.Headers.Add("User-Agent", "AWNHD Weather Broadcast Engine/v1.0 (website; email")
                WB.Headers.Add(HttpRequestHeader.Accept, "application/atom+xml")
                Dim WXData As String = WB.DownloadString($"https://api.weather.gov/alerts/active?zone={My.Settings.Wx_Alerts}")
                If WXData <> LastWXData Then
                    LastWXData = WXData
                    ListBox3.Items.Clear()
                    ListBox3.Enabled = False
                    WXDict.Clear()
                    XmlDoc.LoadXml(WXData)
                    Dim TitleList As XmlNodeList = XmlDoc.GetElementsByTagName("title")
                    Dim CapEvents As XmlNodeList = XmlDoc.GetElementsByTagName("cap:event")
                    Dim CapEventsForColor As XmlNodeList = XmlDoc.GetElementsByTagName("cap:event")
                    Dim SummaryList As XmlNodeList = XmlDoc.GetElementsByTagName("summary")
                    Dim MsgTypeList As XmlNodeList = XmlDoc.GetElementsByTagName("cap:msgType")
                    Dim MsgExpire As XmlNodeList = XmlDoc.GetElementsByTagName("cap:expires")
                    Dim geocode As XmlNodeList = XmlDoc.GetElementsByTagName("cap:areaDesc")

                    For i = 1 To TitleList.Count - 1
                        WXDict.Add(TitleList(i).InnerText, SummaryList(i - 1).InnerText)
                        Label49.Text = TitleList(i).InnerText
                    Next

                    AlertText.Text = Regex.Replace(SummaryList(0).InnerText.Trim(), "(\s*(\r\n|\r|\n)\s*)+", " ")
                    AlertTitle.Text = CapEvents(0).InnerText
                    Label28.Text = CapEvents(0).InnerText
                    Label50.Text = MsgExpire(0).InnerText
                    Label53.Text = geocode(0).InnerText
                    My.Settings.AlertTickerColor = CapEvents(0).InnerText
                    My.Settings.LFIAlert1 = CapEvents(0).InnerText
                    My.Settings.Alert_Message_Title = CapEvents(0).InnerText
                    If MsgTypeList(0).InnerText = "Update" Then
                        AlertTitle.Text = CapEvents(0).InnerText + " Update"
                    End If

                    'If Label53.Text.Contains(My.Settings.CountyName) Then
                    'Panel1.Visible = True
                    'Else
                    '   Panel1.Visible = False
                    'End If

                    'If geocode.Count = 0 Then
                    'Panel1.Visible = False
                    'End If

                    If AlertTitle.Text = "Severe Thunderstorm Warning Update" Then
                        AlertTitle.Text = "Severe T-Storm Warning Update"
                    End If
                    ListBox3.Items.AddRange(WXDict.Keys.ToArray)
                    ListBox3.Enabled = True
                    ListBox3.SelectedIndex = 0

                End If
            Catch ex As Exception
                'MsgBox(ex.Message + " - NOAA_Alerts")
            End Try
        End Using
    End Sub
    Dim LastWXData As String = ""

I have tried everything I can think of. It has something to do with the WxDict not loading. I didn't change any code prior to this and it worked flawlessly. What is going on and how can I fix it? Thanks!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,399 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,580 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,116 Reputation points
    2020-12-29T14:59:00.28+00:00

    Hello @Austin Althouse

    Have you considered using json as a return type with classes to represent the returning json?

    If open to this, add NuGet package Newtonsoft.json -> Install-Package Newtonsoft.Json -Version 12.0.3

    The following was done in a Console project but you can work with this with Windows Forms or WPF. The code is basic code sample showing how to get at various pieces of data strongly typed. I don't iterate each item but there is enough to learn from.

    Classes

    Public Class Geocode  
    	Public Property UGC() As List(Of String)  
    	Public Property SAME() As List(Of String)  
    End Class  
      
    Public Class Reference  
    	<JsonProperty("@id")>  
    	Public Property Id() As String  
    	Public Property identifier() As String  
    	Public Property sender() As String  
    	Public Property sent() As DateTime  
    End Class  
      
    Public Class Parameters  
    	Public Property NWSheadline() As List(Of String)  
    	Public Property VTEC() As List(Of String)  
    	Public Property PIL() As List(Of String)  
    	Public Property BLOCKCHANNEL() As List(Of String)  
    	Public Property eventEndingTime() As List(Of DateTime)  
    	Public Property HazardType() As List(Of String)  
    	<JsonProperty("EAS-ORG")>  
    	Public Property EASORG() As List(Of String)  
    End Class  
      
    Public Class Properties  
    	<JsonProperty("@id")>  
    	Public Property Id() As String  
    	<JsonProperty("@type")>  
    	Public Property Type() As String  
    	Public Property areaDesc() As String  
    	Public Property geocode() As Geocode  
    	Public Property affectedZones() As List(Of String)  
    	Public Property references() As List(Of Reference)  
    	Public Property sent() As DateTime  
    	Public Property effective() As DateTime  
    	Public Property onset() As DateTime  
    	Public Property expires() As DateTime  
    	Public Property ends() As DateTime  
    	Public Property status() As String  
    	Public Property messageType() As String  
    	Public Property category() As String  
    	Public Property severity() As String  
    	Public Property certainty() As String  
    	Public Property urgency() As String  
    	Public Property [event]() As String  
    	Public Property sender() As String  
    	Public Property senderName() As String  
    	Public Property headline() As String  
    	Public Property description() As String  
    	Public Property instruction() As String  
    	Public Property response() As String  
    	Public Property parameters() As Parameters  
    End Class  
      
    Public Class Feature  
    	Public Property id() As String  
    	Public Property type() As String  
    	Public Property geometry() As Object  
    	Public Property properties() As Properties  
    End Class  
      
    Public Class Root  
    	<JsonProperty("@context")>  
    	Public Property Context() As List(Of Object)  
    	Public Property type() As String  
    	Public Property features() As List(Of Feature)  
    	Public Property title() As String  
    	Public Property updated() As DateTime  
    End Class  
    

    Reading back data

    Using webClient As New WebClient  
      
    	webClient.Headers.Add("User-Agent", "AWNHD Weather Broadcast Engine/v1.0 (website; email")  
      
    	Dim json As String = webClient.DownloadString("https://api.weather.gov/alerts/active?area=OR")  
      
    	Dim results As Root = JsonConvert.DeserializeObject(Of Root)(json)  
      
    	Console.WriteLine($"Title: {results.title}")  
      
    	For Each feature As Feature In results.features  
      
    		Console.WriteLine($"Event: {feature.properties.event} " &  
    						  $"Type: {feature.properties.Type.Replace("wx:", "")}")  
      
    		Console.WriteLine($"   Description: {feature.properties.description}")  
      
    		Console.WriteLine(New String("-"c, 30))  
      
    		Console.WriteLine($"{vbTab}Affected zones")  
      
    		For Each affectedZone As String In feature.properties.affectedZones  
    			Console.WriteLine($"{vbTab}{vbTab}{affectedZone}")  
    		Next  
      
    		Console.WriteLine()  
      
    		Console.WriteLine($"{vbTab}geocode.SAME")  
      
    		For Each s As String In feature.properties.geocode.SAME  
    			Console.WriteLine($"{vbTab}{vbTab}{s}")  
    		Next  
      
    		Console.WriteLine()  
      
    	Next  
      
    End Using  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful