Hi,
I'm new to VB, but have played with some programming just a little. I'm working on a Windows application that retrieves XML weather for any location you specify. I'd like to know how to deal with XML to use the data I want. If either is possible, I have no preference whether I analyze each line as it's downloaded or I dump it all to a file and deal with it there. I'm totally new to this, so I don't know how it works to specify what XML you want to retrieve. I'll post the link to a city so you can see what the XML looks like: http://xoap.weather.com/weather/local/USPA1290 cc=*&dayf=5&prod=xoap&par=1017300893&key=268086eb4218acff
Besides not knowing how to refer to any of this in the code to get the info, it just seems like it might be possibly confusing to specify one specific day since there are multiple tags that start with <day d. Thanks for any help.

Several questions
Mbauser
Another quick newbie question. Here's a shortened version of the XML file.
< xml version="1.0" encoding="ISO-8859-1" >
FrankIan
james
aka:Trucker
Alexnaldo Santos
Beugen
What is the most correct solution depends a bit on how large your XML file is. If it is not too large I would choose to work with the XmlDocument class and use XPath expression to extract the nodes that are interesting.
I made a sample project that is just a standard windows application with 2 comboboxes and a button. I added a XML file to the project that looks like this
< xml version="1.0" encoding="utf-8" >
<data>
<comboBox1>
<item text="Item1.1"/>
<item text="Item1.2"/>
<item text="Item1.3"/>
</comboBox1>
<comboBox2>
<item text="Item2.1"/>
<item text="Item2.2"/>
<item text="Item2.3"/>
</comboBox2>
</data>
This is the code in the form.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xmlDoc As System.Xml.XmlDocument
xmlDoc = New System.Xml.XmlDocument
' Default run in projectdir\bin\Debug|Release
' and the xml file is in the projectdir
xmlDoc.Load("..\..\Data.xml")
Dim xmlNodes As System.Xml.XmlNodeList
Dim xmlNode As System.Xml.XmlNode
xmlNodes = xmlDoc.SelectNodes("/data/comboBox1/item/@text")
For Each xmlNode In xmlNodes
ComboBox1.Items.Add(xmlNode.Value.ToString())
Next
xmlNodes = xmlDoc.SelectNodes("/data/comboBox2/item/@text")
For Each xmlNode In xmlNodes
ComboBox2.Items.Add(xmlNode.Value.ToString())
Next
End Sub
As you can see I load the whole xml document in the xmlDoc.Load(). I can then select multiple nodes using a xpath expression with the SelectNodes method. This will give me XmlNodeList that is a collection of XmlNode objects
If you are reading the values from a very large xml document you should use an event driven parsing. The XmlDocument loads everything into memory and can use up too much resources to be efficient.
dehjli
If you're talking about the weather URL, here's the code I have to show the current weather on my form. You can probably see what changes you need to make for it to work for you. This is for a specific form with labels and images.
Public Sub GetWeather(ByVal myzip As String) TryCurrentConditionsLabel.Text =
"" 'Current Weather Dim xmldocument As New System.Xml.XmlDocumentxmldocument =
New System.Xml.XmlDocumentxmldocument.Load(
"http://xoap.weather.com/weather/local/" + myzip + " cc=*&dayf=5") Dim Xnodes As System.Xml.XmlNodeList Dim Xnode As System.Xml.XmlNodeXname = xmldocument.SelectSingleNode(
"/weather/loc/dnam").InnerTextWlocation.Text = Xname
Wlocation2.Text = Xname
Xnode = xmldocument.SelectSingleNode(
"/weather/cc")Xnodes = Xnode.ChildNodes
For Each Xnode In Xnodes If Xnode.Name = "tmp" ThenCurrentTemp.Text = Xnode.InnerText() + Chr(186)
ElseIf Xnode.Name = "flik" ThenCurrentConditionsLabel.Text = CurrentConditionsLabel.Text +
"Feels Like: " + Xnode.InnerText() + Chr(186) + Environment.NewLine ElseIf Xnode.Name = "t" ThenPicCondition.Text = Xnode.InnerText()
ElseIf Xnode.Name = "icon" ThenWeatherStatus.Image = Image.FromFile(
"icons/" & Xnode.InnerText() & ".jpg") ElseIf Xnode.Name = "wind" Then Dim XWindList As System.Xml.XmlNodeList Dim Xwind As System.Xml.XmlNodeXwind = Xnode.SelectSingleNode(
"/weather/cc/wind")XWindList = Xwind.ChildNodes
'Dim Windstring As String = Nothing Dim WindGust As String Dim WindSpeed As String For Each Xwind In XWindList If Xwind.Name = "t" Then Dim WindDir As String = "Wind: From " + Xwind.InnerText If WindGust = "N/A" ThenCurrentConditionsLabel.Text = CurrentConditionsLabel.Text + WindDir + WindSpeed
ElseCurrentConditionsLabel.Text = CurrentConditionsLabel.Text + WindDir + WindSpeed +
" Gusting to " + WindGust + " MPH" End If ElseIf Xwind.Name = "s" ThenWindSpeed =
" at " + Xwind.InnerText + " MPH" 'Windstring = Windstring + "at " + Xwind.InnerText + " MPH" ElseIf Xwind.Name = "gust" ThenWindGust = Xwind.InnerText
'CurrentConditionsLabel.Text = CurrentConditionsLabel.Text + Windstring End If Next End If NextGetForecast(myzip)
Catch ex As ExceptionMessageBox.Show(
"Sorry, we've encountered an error. For debugging purposes (AKA telling Blake), here's the error: " + ex.Message) End Try End SubAnonZZ
YU_MSVB
I've figured out part of my answer. Here's the code.
Private
Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim xmlFile As String = "C:\Documents and Settings\Owner\Desktop\weather.xml" Dim xmlTr As New XmlTextReader(xmlFile) While xmlTr.Read If xmlTr.Name = "dnam" AndAlso xmlTr.NodeType = XmlNodeType.Element ThenListBox1.Items.Add(xmlTr.ReadString)
ElseIf xmlTr.Name = "t" ThenListBox1.Items.Add(xmlTr.ReadString)
ElseIf xmlTr.Name = "flik" ThenListBox1.Items.Add(xmlTr.ReadString)
ElseIf xmlTr.Name = "s" ThenListBox1.Items.Add(xmlTr.ReadString)
End If End WhilexmlTr.Close()
End SubNow I need to figure out how to specify which information I want when some of the tag names are used more than once. Sorry to make this long, but I want to post some XML to show. I just don't know how to specify a certain parent/child relationship, since I figure that's how it's done. Notice how "t" is used under cc and wind (and also other places). How would I specify if I only wanted "t" if it were under wind for example Thanks
dimple_dimple
I get a "The remote server returned an error: (407) Proxy Authentication Required." error message at the following line:
xmldocument.Load("
http://xoap.weather.com/weather/local/49707 cc=*&dayf=5")Any ideas
Lizard Man
Thanks for the help. I have yet to try that method, but I plan to. The XML file is pretty small, so that shouldn't be a problem. I do have one question. I saw something similar to this earlier and wondered what the @ sign in a line like this means:
xmlDoc.SelectNodes(/data/comboBox2/item/@text)
Does that mean you're referring to an attribute If I'm not going to refer to an attribute in the XML file, do I leave that out Thanks.
Shyam Vaidya
You are correct the @ sign means I am looking for a attribute named "text" in the element named "item".
If you would just like to get the element named "item" the xpath would look like this "/data/comboBox2/item" and the value of the node would be the text inside the element.
bianrq
Abid Ch.
Chris Z.
byerh
It is enough to write it like "/weather/loc/dname" but if you want to be sure that it is of a certain weather "ver" or loc "id" you can write it like this
"/weather[@ver='2.0']/loc[@id='ZIP goes here']/dnam"
The value of the node will be "City Name is Here"