Partial Class WeatherBug Inherits System.Web.UI.Page Dim ApiCode As String = "A0000000000" 'Get an API Code by going here: http://weather.weatherbug.com/desktop-weather/api-eula.html Dim WithEvents GetWeatherButton As New Button Dim ZipCodeTextBox As New TextBox Dim WeatherDataLabel As New Label
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load GetWeatherButton.Text = "Get Weather >" Page.Form.Controls.Add(GetWeatherButton) Dim ZipCodeLabel As New Label ZipCodeLabel.Text = " Zip Code: " Page.Form.Controls.Add(ZipCodeLabel) ZipCodeTextBox.Text = "20876" Page.Form.Controls.Add(ZipCodeTextBox) Dim BlankLineLabel As New Label BlankLineLabel.Text = "<br> <br>" Page.Form.Controls.Add(BlankLineLabel) WeatherDataLabel.Text = "" Page.Form.Controls.Add(WeatherDataLabel) End Sub
Protected Sub GetWeatherButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GetWeatherButton.Click If ZipCodeTextBox.Text <> "" Then Dim GetWebData As New Net.WebClient Dim XmlText As String = GetWebData.DownloadString("http://api.wxbug.net/getLiveWeatherRSS.aspx?ACode=" & ApiCode & "&zipcode=" & Trim(ZipCodeTextBox.Text)) Dim CurrentWeatherDataView As Data.DataView = XmlText2DataView(XmlText, "item") If CurrentWeatherDataView.Count > 0 Then WeatherDataLabel.Text = CurrentWeatherDataView.Table.Rows(0)("description") 'Display a link to the WeatherBug website for more information. THIS IS REQUIRED IN ORDER TO USE THE API. WeatherDataLabel.Text += "<a target='_blank' href='" & CurrentWeatherDataView.Table.Rows(0)("link") & "'>" & CurrentWeatherDataView.Table.Rows(0)("title") & "</a>" End If End If End Sub
Function XmlText2DataView(ByVal XmlText As String, Optional ByVal TableName As String = "") As Data.DataView Dim XmlDataSet As New Data.DataSet Dim CharacterEncoding As New UTF8Encoding Dim ByteData As Byte() = CharacterEncoding.GetBytes(XmlText) Dim MemoryStreamData As New IO.MemoryStream(ByteData) Dim StreamData As IO.Stream = CType(MemoryStreamData, IO.Stream) Try XmlDataSet.ReadXml(StreamData) Catch End Try If (TableName = "") And (XmlDataSet.Tables.Count > 0) Then TableName = XmlDataSet.Tables(0).TableName End If Dim XmlDataView As New Data.DataView(XmlDataSet.Tables(TableName)) Return XmlDataView End Function
End Class |
No comments:
Post a Comment