Finding if XML node exists in System.Xml.XmlDocument

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I have this code that parses an XML feed online to get the weather for any US zip code. Because of our current weather, I just found out that if there is a sever weather advisory, there is a new node with child nodes describing the condition. I haven't worked with VB in awhile, and I was wondering how to write the code to find if a specific node exists. Here's the code I have now, before any changes to get this working.
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] GetWeather([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Zip [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Len(Zip) < 6 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xmldocument [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument.Load([/SIZE][SIZE=2][COLOR=#800000]"http://xoap.weather.com/weather/local/"[/COLOR][/SIZE][SIZE=2] + Zip + [/SIZE][SIZE=2][COLOR=#800000]"?cc=*&dayf=5&prod=xoap&par=1017300893&key=268086eb4218acff"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xnodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNodeList
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xnode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xname [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]Xname = xmldocument.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/loc/dnam"[/COLOR][/SIZE][SIZE=2]).InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] newxname [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = Mid(Xname, 1, Xname.Length - 8)
newzipone = Mid(Zip, 1, 1)
newziptwo = Mid(Zip, 2, 1)
newzipthree = Mid(Zip, 3, 1)
newzipfour = Mid(Zip, 4, 1)
newzipfive = Mid(Zip, 5, 1)
weatherstring = [/SIZE][SIZE=2][COLOR=#800000]"Weather for "[/COLOR][/SIZE][SIZE=2] & newzipone & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newziptwo & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newzipthree & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newzipfour & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newzipfive & [/SIZE][SIZE=2][COLOR=#800000]"."
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newxname & [/SIZE][SIZE=2][COLOR=#800000]"."
[/COLOR][/SIZE][SIZE=2]Xnode = xmldocument.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/cc"[/COLOR][/SIZE][SIZE=2])
Xnodes = Xnode.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] Xnode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] Xnodes
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Xnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"tmp"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & [/SIZE][SIZE=2][COLOR=#800000]" Temperature: "[/COLOR][/SIZE][SIZE=2] & Xnode.InnerText() & [/SIZE][SIZE=2][COLOR=#800000]"."
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"flik"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & [/SIZE][SIZE=2][COLOR=#800000]" Feels Like: "[/COLOR][/SIZE][SIZE=2] + Xnode.InnerText() & [/SIZE][SIZE=2][COLOR=#800000]"."
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"t"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & [/SIZE][SIZE=2][COLOR=#800000]" Conditions: "[/COLOR][/SIZE][SIZE=2] & Xnode.InnerText() & [/SIZE][SIZE=2][COLOR=#800000]","
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"wind"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] XWindList [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNodeList
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xwind [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNode
Xwind = Xnode.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/cc/wind"[/COLOR][/SIZE][SIZE=2])
XWindList = Xwind.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] Xwind [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] XWindList
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Xwind.Name = [/SIZE][SIZE=2][COLOR=#800000]"t"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] WindDir [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#800000]" Winnd: From "[/COLOR][/SIZE][SIZE=2] + Xwind.InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xwind.Name = [/SIZE][SIZE=2][COLOR=#800000]"s"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Xwind.InnerText = [/SIZE][SIZE=2][COLOR=#800000]"calm"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]windspeed = [/SIZE][SIZE=2][COLOR=#800000]"Winnd is calm"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]windspeed = [/SIZE][SIZE=2][COLOR=#800000]"Winnd at "[/COLOR][/SIZE][SIZE=2] + Xwind.InnerText + [/SIZE][SIZE=2][COLOR=#800000]" miles per hour"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]windstring = windstring + [/SIZE][SIZE=2][COLOR=#800000]"at "[/COLOR][/SIZE][SIZE=2] + Xwind.InnerText + [/SIZE][SIZE=2][COLOR=#800000]" miles per hour"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] Xwind.Name = [/SIZE][SIZE=2][COLOR=#800000]"gust"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Xwind.InnerText = [/SIZE][SIZE=2][COLOR=#800000]"N/A"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]windgust = [/SIZE][SIZE=2][COLOR=#800000]"With no gusting wind"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]windgust = [/SIZE][SIZE=2][COLOR=#800000]"Gusting to "[/COLOR][/SIZE][SIZE=2] & Xwind.InnerText & [/SIZE][SIZE=2][COLOR=#800000]"miles per hour"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]weatherstring = weatherstring & winddir & windspeed & windgust
sayweather(weatherstring)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

The XML I want is the <swa> node, specifically the values of its child nodes. Here's what the XML looks like up through that node. Thanks for any help.
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <!-- This document is intended only for use by authorized licensees of The Weather Channel. Unauthorized use is prohibited. Copyright 1995-2005, The Weather Channel Interactive, Inc. All Rights Reserved. -->f(clean);
- <weather ver="2.0">
- <head>
<locale>en_US</locale>

<form>MEDIUM</form>

<ut>F</ut>

<ud>mi</ud>

<us>mph</us>

<up>in</up>

<ur>in</ur>

</head>


- <loc id="30117">
<dnam>Carrollton, GA (30117)</dnam>

<tm>10:15 PM</tm>

<lat>33.58</lat>

<lon>-85.1</lon>

<sunr>7:13 AM</sunr>

<suns>5:37 PM</suns>

<zone>-5</zone>

</loc>


- <swa>
- <a id="5540" uc="2">
<t>RIVER FLOOD WARNING FOR CARROLL COUNTY, COWETA COUNTY, HEARD COUNTY, GA UNTIL THU NOV 16 2006 01:15 PM EST</t>

<l>http://www.weather.com/weather/alerts?alertId=5540</l>

</a>


- <a id="5888" uc="0">
<t>WIND ADVISORY UNTIL THU NOV 16 2006 04:00 AM EST</t>

<l>http://www.weather.com/weather/alerts?alertId=5888</l>

</a>


</swa>





 
One tip for posting Xml, don't copy what's displayed in IE Browser window, it contains all kinds of irrelevant formatting for the display. Instead do View Source which opens the pure Xml source in Notepad and copy it from there. Viewing online source in IE is still usefull for you as the navigation tree is easy to read for getting paths, and you can actively expand and collapse nodes.

The path to what you ask is all "/Weather/swa/a" nodes so SelectNodes this. For Each of these nodes you SelectSingleNode "t" for text and "l" for link.
 
Thanks for the tips. I have to leave for work, so I can't try this out right now, but I have one question. <swa> doesn't always exist in the XML, only if there is severe weather, so won't that give me an error if I always try to search for the child nodes of swa? Is there a way to tell it to get the child nodes only if swa exists? Thanks.
 
No, SelectNodes method does the specified Xpath query and return a XmlNodeList. If no nodes is found the XmlNodeList will have a Count of zero.
 
I just started working on this again and I'm still having problems getting the severe weather alerts out of the XML. It should be easy, but I can't figure it out. In the following section, I just want the InnerText in "t":

<swa>
- <a id="98096" uc="0">
<t>WIND ADVISORY UNTIL FRI DEC 01 2006 11:15 AM EST</t>

<l>http://www.weather.com/weather/alerts?alertId=98096</l>

</a>


</swa>


I saved a copy of the full XML page which you can see at http://blakenet.myftp.org/weather.xml Thanks.

 
Funny I figured this out as soon as I posted. I don't know if something was getting confused by having it check for the current weather and severe weather in the same subroutine, but I made a new subroutine that would run on a timer interval and it works. This is what I have, if anyone is interested.
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] severecheck()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xmldocument [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument.Load([/SIZE][SIZE=2][COLOR=#800000]"C:\Documents and Settings\Owner\Desktop\30117.xml"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xnode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNode = xmldocument.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/swa/a"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] snode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xnodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Xml.XmlNodeList = Xnode.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] snode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] Xnodes
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] snode.Name = [/SIZE][SIZE=2][COLOR=#800000]"t"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] s [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SpeechLib.SpVoice
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fs [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SpeechLib.SpFileStream
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] setformat [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SpeechLib.SpAudioFormat
setformat.Type = SpeechLib.SpeechAudioFormatType.SAFT8kHz8BitMono
fs.Format.Type = setformat.Type
fs.Open([/SIZE][SIZE=2][COLOR=#800000]"C:\weatherstuff\alert.wav"[/COLOR][/SIZE][SIZE=2], SpeechLib.SpeechStreamFileMode.SSFMCreateForWrite)
s.AudioOutputStream = fs
s.Voice = s.GetVoices.Item(0)
s.Speak([/SIZE][SIZE=2][COLOR=#800000]"This is a test of the severe weather system"[/COLOR][/SIZE][SIZE=2])
TextBox4.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2]fs.Close()
EchoLink.PlayWAVFile([/SIZE][SIZE=2][COLOR=#800000]"C:\weatherstuff\alert.wav"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
There is one problem with this. If there are no weather advisories, then the XML node doesn't exist, and my program errors out. How can I check to see if it's there so this doesn't happen?
VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] severecheck()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xmldocument [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument.Load([/SIZE][SIZE=2][COLOR=#800000]"http://xoap.weather.com/weather/local/"[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.DefZIP & [/SIZE][SIZE=2][COLOR=#800000]"?cc=*&dayf=5"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]'xmldocument.Load("C:\Documents and Settings\Owner\Desktop\30117.xml")
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xnode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNode = xmldocument.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/swa"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xnodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = Xnode.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] Xnode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] xnodes
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] subnodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = Xnode.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] newnode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] newnode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] subnodes
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] newnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"t"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]x = x + 1
severestatement = severestatement & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newnode.InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] nsubnodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = newnode.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] x > 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]severestatement = [/SIZE][SIZE=2][COLOR=#800000]"There are "[/COLOR][/SIZE][SIZE=2] & x & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#800000]"advisories: "[/COLOR][/SIZE][SIZE=2] & severestatement
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]severestatement = [/SIZE][SIZE=2][COLOR=#800000]"There is one advisory: "[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & severestatement
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] s [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SpeechLib.SpVoice
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fs [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SpeechLib.SpFileStream
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] setformat [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SpeechLib.SpAudioFormat
setformat.Type = SpeechLib.SpeechAudioFormatType.SAFT8kHz8BitMono
fs.Format.Type = setformat.Type
fs.Open([/SIZE][SIZE=2][COLOR=#800000]"C:\weatherstuff\alert.wav"[/COLOR][/SIZE][SIZE=2], SpeechLib.SpeechStreamFileMode.SSFMCreateForWrite)
s.AudioOutputStream = fs
s.Voice = s.GetVoices.Item(0)
s.Speak(severestatement)
TextBox4.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2]fs.Close()
EchoLink.PlayWAVFile([/SIZE][SIZE=2][COLOR=#800000]"C:\weatherstuff\alert.wav"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

 
It all works now.

VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] severecheck()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sva [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xmldocument [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Xml.XmlDocument
xmldocument.Load([/SIZE][SIZE=2][COLOR=#800000]"http://xoap.weather.com/weather/local/"[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.DefZIP & [/SIZE][SIZE=2][COLOR=#800000]"?cc=*&dayf=5"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]'xmldocument.Load("C:\Documents and Settings\Owner\Desktop\30117.xml")
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Xnode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNode = xmldocument.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/weather/swa"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xnodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = Xnode.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] Xnode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] xnodes
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] subnodes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNodeList = Xnode.ChildNodes
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] newnode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Xml.XmlNode
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] newnode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] subnodes
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] newnode.Name = [/SIZE][SIZE=2][COLOR=#800000]"t"[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sva = sva + 1
severestatement = severestatement & [/SIZE][SIZE=2][COLOR=#800000]"....."[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & newnode.InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Dim nsubnodes As Xml.XmlNodeList = newnode.ChildNodes
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
sva = 0
IO.File.AppendAllText([/SIZE][SIZE=2][COLOR=#800000]"C:\weatherstuff\errorlog.txt"[/COLOR][/SIZE][SIZE=2], DateTime.Now.ToShortDateString & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & DateTime.Now.ToShortTimeString & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & ex.Message & Environment.NewLine)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
 
[/COLOR][/SIZE][SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] sva > 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]severestatement = [/SIZE][SIZE=2][COLOR=#800000]"There are "[/COLOR][/SIZE][SIZE=2] & sva & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#800000]"advisories: "[/COLOR][/SIZE][SIZE=2] & severestatement
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] sva = 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]severestatement = [/SIZE][SIZE=2][COLOR=#800000]"There is one advisory: "[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#800000]" "[/COLOR][/SIZE][SIZE=2] & severestatement
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]severestatement = [/SIZE][SIZE=2][COLOR=#800000]"There are no severe weather advisories"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] s [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SpeechLib.SpVoice
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fs [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SpeechLib.SpFileStream
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] setformat [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SpeechLib.SpAudioFormat
setformat.Type = SpeechLib.SpeechAudioFormatType.SAFT8kHz8BitMono
fs.Format.Type = setformat.Type
fs.Open([/SIZE][SIZE=2][COLOR=#800000]"C:\weatherstuff\alert.wav"[/COLOR][/SIZE][SIZE=2], SpeechLib.SpeechStreamFileMode.SSFMCreateForWrite)
s.AudioOutputStream = fs
s.Voice = s.GetVoices.Item(0)
s.Speak(severestatement)
sva = 0
severestatement = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2]TextBox4.Text = [/SIZE][SIZE=2][COLOR=#800000]""
[/COLOR][/SIZE][SIZE=2]fs.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] EchoLink.PTT = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]severesaywait.Interval = 300
severesaywait.Start()
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] EchoLink.Receiving = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]severesaywait.Interval = 300
severesaywait.Start()
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]EchoLink.PlayWAVFile([/SIZE][SIZE=2][COLOR=#800000]"C:\weatherstuff\echogreeting.wav"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])
severesaywait.Stop()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]EchoLink.PlayWAVFile([/SIZE][SIZE=2][COLOR=#800000]"C:\weatherstuff\alert.wav"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

 
You should do this:
VB.NET:
[SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] Xnode [COLOR=blue]As[/COLOR] Xml.XmlNode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] xmldocument.SelectNodes([SIZE=2][COLOR=#800000]"/weather/swa/a"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[/SIZE]
[SIZE=2][COLOR=blue]Next[/COLOR][/SIZE]
The Xpath expression "/weather/swa/a" will select all "a" nodes in path "/weather/swa", if expression return no nodes the SelectNodes return an empty NodeList, so the For Each iteration will not process anything in that case. So you don't need to Try or check if previous path is Nothing.

This was really explained to you already in post 2 and 4.
 
Back
Top