Several questions

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.



Answer this question

Several questions

  • Spock101

    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.



  • Ripon12

    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.


  • bdubyah

    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


  • Robert Dunlop

    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.



  • mclagett

    Thanks. :) Rather than posting the whole code for this here, I put it as a page on my website since it's long. You can look at it at http://blakenet.myftp.org/weathercode.doc. Are we able to include images in these posts I can't see how to do it if we are. Anyway, you can see a screenshot of the app on my home page at http://blakenet.myftp.org. This is the first VB program I wrote. I added more features to it lately. It now has a feature that lets you select which US zip code you want to see the weather for when the app starts and also lets you select a "Balloon zip code", which is displayed in a tooltip icon when you hover over the icon in the system tray. This lets you keep the app minimized and still check the weather. I originally only had the ability to let the user save three zip codes, but I learned more and now it's unlimited. Hope you enjoy it. If you live in the US (or know US zip codes, you can get the zip file for the program at http://blakenet.myftp.org/X-Weather.zip. I'm not sure, but I think that build is the latest one. If it shows a screen making you sign up for a license key, let me know and I'll put up the latest build.
  • ANDERSLIND

    Hmm, that's odd, because that URL works for me.
  • g.neeraj

    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)

    Try

    CurrentConditionsLabel.Text = ""

    'Current Weather

    Dim xmldocument As New System.Xml.XmlDocument

    xmldocument = New System.Xml.XmlDocument

    xmldocument.Load("http://xoap.weather.com/weather/local/" + myzip + " cc=*&dayf=5")

    Dim Xnodes As System.Xml.XmlNodeList

    Dim Xnode As System.Xml.XmlNode

    Xname = xmldocument.SelectSingleNode("/weather/loc/dnam").InnerText

    Wlocation.Text = Xname

    Wlocation2.Text = Xname

    Xnode = xmldocument.SelectSingleNode("/weather/cc")

    Xnodes = Xnode.ChildNodes

    For Each Xnode In Xnodes

    If Xnode.Name = "tmp" Then

    CurrentTemp.Text = Xnode.InnerText() + Chr(186)

    ElseIf Xnode.Name = "flik" Then

    CurrentConditionsLabel.Text = CurrentConditionsLabel.Text + "Feels Like: " + Xnode.InnerText() + Chr(186) + Environment.NewLine

    ElseIf Xnode.Name = "t" Then

    PicCondition.Text = Xnode.InnerText()

    ElseIf Xnode.Name = "icon" Then

    WeatherStatus.Image = Image.FromFile("icons/" & Xnode.InnerText() & ".jpg")

    ElseIf Xnode.Name = "wind" Then

    Dim XWindList As System.Xml.XmlNodeList

    Dim Xwind As System.Xml.XmlNode

    Xwind = 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" Then

    CurrentConditionsLabel.Text = CurrentConditionsLabel.Text + WindDir + WindSpeed

    Else

    CurrentConditionsLabel.Text = CurrentConditionsLabel.Text + WindDir + WindSpeed + " Gusting to " + WindGust + " MPH"

    End If

    ElseIf Xwind.Name = "s" Then

    WindSpeed = " at " + Xwind.InnerText + " MPH"

    'Windstring = Windstring + "at " + Xwind.InnerText + " MPH"

    ElseIf Xwind.Name = "gust" Then

    WindGust = Xwind.InnerText

    'CurrentConditionsLabel.Text = CurrentConditionsLabel.Text + Windstring

    End If

    Next

    End If

    Next

    GetForecast(myzip)

    Catch ex As Exception

    MessageBox.Show("Sorry, we've encountered an error. For debugging purposes (AKA telling Blake), here's the error: " + ex.Message)

    End Try

    End Sub


  • Thomas Kristensen

    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"

     



  • dale_barr

    Blake81 wrote:
    Hmm, that's odd, because that URL works for me.
    Works for me too! I like this one. How about posting the code for GetForecast(myzip) I have played around with RSS feeds off and on and the Weather forecasts are the most fun.

    james

    aka:Trucker


  • ThankYouDriveThrough

    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 Then

    ListBox1.Items.Add(xmlTr.ReadString)

    ElseIf xmlTr.Name = "t" Then

    ListBox1.Items.Add(xmlTr.ReadString)

    ElseIf xmlTr.Name = "flik" Then

    ListBox1.Items.Add(xmlTr.ReadString)

    ElseIf xmlTr.Name = "s" Then

    ListBox1.Items.Add(xmlTr.ReadString)

    End If

    End While

    xmlTr.Close()

    End Sub

    Now 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

    - <cc>
      <lsup>2/21/06 11:25 AM EST</lsup>
      <obst>Oil City, PA</obst>
      <tmp>28</tmp>
      <flik>17</flik>
      <t>Cloudy</t>
      <icon>26</icon>
    - <bar>
      <r>30.03</r>
      <d>rising</d>
      </bar>
    - <wind>
      <s>16</s>
      <gust>20</gust>
      <d>240</d>
      <t>WSW</t>
      </wind>

     


  • Der M

    How can I retrieve the data from the link in the first post in this thread so that I can display it on a form I am just looking for the code to retrieve the xml data from a URL. If someone can post a snippet I would be very greatful. Thanks...
  • Matt Palmerlee

    Another quick newbie question. Here's a shortened version of the XML file.

    < 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.
    -->
    - <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="ZIP goes here">
    <dnam>City Name is Here</dnam>
    <tm>5:10 PM</tm>
    <lat>33.58</lat>
    <lon>-85.1</lon>
    <sunr>7:18 AM</sunr>
    <suns>6:30 PM</suns>
    <zone>-5</zone>
    </loc>
    What would the XPath be if I wanted to get the data stored in <dnam> I wasn't sure if I should include the <weather ver="2.0"> or not in the XPath. Thanks.

  • gbm1

    Thanks, I will give your code a try...
  • Mange

    If I enter that exact URL into Internet Explorer the data comes up fine, but for some reason it doesn't work in VB, it just gives me that error...
  • Several questions