Purpose Display data read via a web service |
Solution As an example we read weather data for various cities using the web sevice http://api.openweathermap.org. We use the XMLReader object of .NET for reading and parsing the XML data returned by the web service. The final result looks as follows:
// build up variables with city names Clear V[myweather.cell.*] // Cities Set V[myweather.cell.city.2] "Hamburg,DE" Set V[myweather.cell.city.3] "Dortmund,DE" Set V[myweather.cell.city.4] "Heidelberg,DE" Set V[myweather.cell.city.5] "Roma,IT" Set V[myweather.cell.city.6] "Sydney,AU" Set V[myweather.cell.city.7] "Los Angeles,US" Set V[myweather.cell.city.8] "New York,US" Set V[myweather.cell.city.9] "Las Vegas,US" Set V[myweather.cell.city.10] "Quebec,CA" // read weather data for each city label next_cityif V[myweather.cell.city.&V[k]] // reset variables Clear V[response.*] CallVB "utilities.connect.weather" "&V[myweather.cell.city.&V[k]]" "response" // weather data found? if V[response.temperature.value] Set V[myweather.cell.temperature.&V[k]] "&V[response.temperature.value]°" Set V[myweather.cell.clouds.&V[k]] "&V[response.clouds.name]" Set V[myweather.cell.wind.&V[k]] _ "&V[response.speed.name], &V[response.speed.value] km/h" Set V[myweather.cell.humidity.&V[k]] "&V[response.humidity.value]%" endif set
V[k]
&V[k]
+ 1 endif // table row count Table // add button to transfer new readings VB.NET function Public Class connect Private guixt As New guinet.guixt Public Sub weather(city As String, varname As String) ' build URL according to open weather API, for the given city Dim url As String = _ "http://api.openweathermap.org/data/2.5/weather?q=" & _ city & _ "&APPID=15c03608ece9fc377a13a6cd2cxxxxxx&mode=xml" & _ "&units=metric&lang=en" ' API returns XML stream with weather data Dim XMLReader As New Xml.XmlTextReader(url) With XMLReader Do While .Read Select Case .NodeType Case Xml.XmlNodeType.Element Dim xname As String = .LocalName If .AttributeCount > 0 Then While .MoveToNextAttribute ' next attribute ' set GuiXT variable guixt.SetVariable _ (varname & "." & xname & _ "." & .LocalName, .Value) End While End If End Select Loop .Close() End With End Sub End Class |
Components |