Imports System.Net Imports System.io Imports System.Xml Imports System.Xml.XPath Public Class Simepar Private myRequest As WebRequest Private myResponse As WebResponse Private VarStream As Stream Private xmlreader As XmlTextReader Private xpathDoc As XPathDocument Public Function GetCidades() As DataTable LeXML() Dim DT As New DataTable("municipio") DT.Columns.Add(New DataColumn("id", System.Type.GetType("System.String"))) DT.Columns.Add(New DataColumn("nome", System.Type.GetType("System.String"))) Dim xmlNav As XPathNavigator Try xmlNav = xpathDoc.CreateNavigator() Catch ex As XPathException Throw ex Catch ex As Exception Throw ex End Try Dim xmlNI As XPathNodeIterator xmlNI = xmlNav.Select("/boletim/municipio") While (xmlNI.MoveNext()) If xmlNI.Current.HasAttributes Then Dim DR As DataRow = DT.NewRow xmlNI.Current.MoveToFirstAttribute() Do If xmlNI.Current.Name = "id" Then DR("id") = xmlNI.Current.Value 'HttpContext.Current.Response.Write("id:" & xmlNI.Current.Value & "
") End If If xmlNI.Current.Name = "nome" Then DR("nome") = xmlNI.Current.Value ' HttpContext.Current.Response.Write("nome:" & xmlNI.Current.Value & "
") End If Loop While xmlNI.Current.MoveToNextAttribute DT.Rows.Add(DR) End If End While Return DT End Function Public Function getTempoAmanha(ByVal id As String) As DataTable LeXML() Dim DT As New DataTable("tempoAmanha") DT.Columns.Add(New DataColumn("data", System.Type.GetType("System.String"))) DT.Columns.Add(New DataColumn("diadasemana", System.Type.GetType("System.String"))) DT.Columns.Add(New DataColumn("icone", System.Type.GetType("System.String"))) DT.Columns.Add(New DataColumn("descricao", System.Type.GetType("System.String"))) DT.Columns.Add(New DataColumn("min", System.Type.GetType("System.String"))) DT.Columns.Add(New DataColumn("max", System.Type.GetType("System.String"))) DT.Columns.Add(New DataColumn("direcao", System.Type.GetType("System.String"))) DT.Columns.Add(New DataColumn("intensidade", System.Type.GetType("System.String"))) DT.Columns.Add(New DataColumn("visibilidade", System.Type.GetType("System.String"))) Dim xmlNav As XPathNavigator Try xmlNav = xpathDoc.CreateNavigator() Catch ex As XPathException Throw ex Catch ex As Exception Throw ex End Try Dim DR As DataRow = DT.NewRow DR("data") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/@data", xmlNav) DR("diadasemana") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/@diadasemana", xmlNav) DR("icone") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/tempo/legendadia/@icone", xmlNav) If IsDBNull(DR("icone")) Then DR("icone") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/tempo/legendamanha/@icone", xmlNav) End If DR("descricao") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/tempo/legendadia", xmlNav) If IsDBNull(DR("descricao")) Then DR("descricao") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/tempo/legendamanha", xmlNav) End If DR("min") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/temperatura/min", xmlNav) DR("max") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/temperatura/max", xmlNav) DR("direcao") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/vento/direcao", xmlNav) DR("intensidade") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/vento/intensidade", xmlNav) DR("visibilidade") = getNIValue("/boletim/municipio[@id=" & id & "]/dia2/visibilidade", xmlNav) DT.Rows.Add(DR) Return DT End Function Public Function getTempoCompleto(ByVal id As String) As DataSet LeXML() Dim DTTempo4Dias As New DataTable("tempoAmanha") DTTempo4Dias.Columns.Add(New DataColumn("data", System.Type.GetType("System.String"))) DTTempo4Dias.Columns.Add(New DataColumn("diadasemana", System.Type.GetType("System.String"))) DTTempo4Dias.Columns.Add(New DataColumn("icone", System.Type.GetType("System.String"))) DTTempo4Dias.Columns.Add(New DataColumn("descricao", System.Type.GetType("System.String"))) DTTempo4Dias.Columns.Add(New DataColumn("min", System.Type.GetType("System.String"))) DTTempo4Dias.Columns.Add(New DataColumn("max", System.Type.GetType("System.String"))) DTTempo4Dias.Columns.Add(New DataColumn("direcao", System.Type.GetType("System.String"))) DTTempo4Dias.Columns.Add(New DataColumn("intensidade", System.Type.GetType("System.String"))) DTTempo4Dias.Columns.Add(New DataColumn("visibilidade", System.Type.GetType("System.String"))) Dim xmlNav As XPathNavigator Try xmlNav = xpathDoc.CreateNavigator() Catch ex As XPathException Throw ex Catch ex As Exception Throw ex End Try For dia As Integer = 2 To 5 Dim DR As DataRow = DTTempo4Dias.NewRow DR("data") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/@data", xmlNav) DR("diadasemana") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/@diadasemana", xmlNav) DR("icone") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/tempo/legendadia/@icone", xmlNav) If IsDBNull(DR("icone")) Then DR("icone") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/tempo/legendamanha/@icone", xmlNav) End If DR("descricao") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/tempo/legendadia", xmlNav) If IsDBNull(DR("descricao")) Then DR("descricao") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/tempo/legendamanha", xmlNav) End If DR("min") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/temperatura/min", xmlNav) DR("max") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/temperatura/max", xmlNav) DR("direcao") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/vento/direcao", xmlNav) DR("intensidade") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/vento/intensidade", xmlNav) DR("visibilidade") = getNIValue("/boletim/municipio[@id=" & id & "]/dia" & dia & "/visibilidade", xmlNav) DTTempo4Dias.Rows.Add(DR) Next Dim DtCondicoes As New DataTable("condicoes") DtCondicoes.Columns.Add(New DataColumn("data", System.Type.GetType("System.String"))) DtCondicoes.Columns.Add(New DataColumn("diadasemana", System.Type.GetType("System.String"))) DtCondicoes.Columns.Add(New DataColumn("descricao", System.Type.GetType("System.String"))) For dia As Integer = 1 To 2 Dim DRCondicoes As DataRow = DtCondicoes.NewRow DRCondicoes("data") = getNIValue("/boletim/condicoes/dia" & dia & "/@data", xmlNav) DRCondicoes("diadasemana") = getNIValue("/boletim/condicoes/dia" & dia & "/@diadasemana", xmlNav) DRCondicoes("descricao") = getNIValue("/boletim/condicoes/dia" & dia, xmlNav) DtCondicoes.Rows.Add(DRCondicoes) Next Dim Ds As New DataSet("Simepar") Ds.Tables.Add(DTTempo4Dias) Ds.Tables.Add(DtCondicoes) Return Ds End Function Public Function getNIValue(ByVal selection As String, ByRef xmlNav As XPathNavigator) As String Dim xmlNI As XPathNodeIterator xmlNI = xmlNav.Select(selection) While (xmlNI.MoveNext()) Return xmlNI.Current.Value() End While End Function Public Function GetData(ByVal id As String) As DataTable LeXML() Dim DT As New DataTable("municipio") End Function Private Sub LeXML() If HttpContext.Current.Cache("xpathDoc") Is Nothing Then myRequest = WebRequest.Create("http://www.simepar.br/tempo/boletimxml.jsp") myResponse = myRequest.GetResponse() VarStream = myResponse.GetResponseStream() xmlreader = New XmlTextReader(VarStream) xpathDoc = New XPathDocument(xmlreader) HttpContext.Current.Cache.Insert("xpathDoc", xpathDoc, Nothing, Now.AddMinutes(60), Caching.Cache.NoSlidingExpiration) Else xpathDoc = CType(HttpContext.Current.Cache("xpathDoc"), XPathDocument) End If End Sub End Class