Con este scrip de vb.net podemos extraer los datos de la agencia estatal de meteorología para conseguir una central meteorologica virtual.
También nos sirve de ejemplo para entender la clase XmlReader de .net , el cual representa un lector que proporciona acceso rápido a datos XML, sin almacenamiento en caché y con desplazamiento solo hacia delante.
Los datos extradidos son:
- Hora de Orto (salida de sol)
- Hora de Ocaso (Puesta de sol)
- Precipitación Actual
- Precipitación todo el día
- Temperatura
- Humedad
- Velocidad de viento
- Dirección de viento
Siéntase libre de utilizar este código en sus proyecto, se entrega totalmente libre.
Public orto, ocaso As String Public precipitacion, nieve, temperatura, humedad_relativa, VientoDir, WindSpeed As String Public PrecipitacionTotal As Double Public Sub GetAEMET() Dim per As String = Date.Now.Hour.ToString.PadLeft(2, "0") Using reader As XmlReader = XmlReader.Create(FileMeteo) reader.ReadToFollowing("dia") Dim d As String = reader.GetAttribute("fecha") If Not IsNothing(d) Then Dim dt As Date = CDate(d) If dt = Date.Today Then orto = reader.GetAttribute("orto") ocaso = reader.GetAttribute("ocaso") reader.MoveToFirstAttribute() PrecipitacionTotal = 0 While reader.Read Select Case reader.Name Case "precipitacion" Dim k As String = reader.GetAttribute("periodo") If IsNothing(k) = False Then Dim v As String = reader.ReadElementContentAsString() If IsNumeric(v) Then If k = per Then precipitacion = v End If PrecipitacionTotal += CDbl(Strings.Replace(v, ".", ",")) End If End If Case "nieve" GetValue(reader, per, nieve) Case "temperatura" GetValue(reader, per, temperatura) Case "humedad_relativa" GetValue(reader, per, humedad_relativa) Case "viento" Dim k As String = reader.GetAttribute("periodo") If IsNothing(k) = False Then If k = per Then reader.ReadToFollowing("direccion") Dim v As String = reader.ReadElementContentAsString() If v.Length > 0 Then VientoDir = v reader.ReadToFollowing("velocidad") v = reader.ReadElementContentAsString() If v.Length > 0 Then WindSpeed = v End If End If Case "dia" Exit While End Select End While End If End If End Using End Sub Private Sub GetValue(reader As XmlReader, Per As String, ByRef Value As String) Dim k As String = reader.GetAttribute("periodo") If IsNothing(k) = False Then If k = Per Then Dim v As String = reader.ReadElementContentAsString() If v.Length > 0 Then Value = v End If End If End If End Sub