Weather Update on your Mobile in VB.NET

Description

In this article,  I would like to discuss about  "Weather Update" on your mobile phone. When we get up in the early morning , everyone  curious to know about the weather . I hope that it would be very useful to everyone to make a plan for a day.  

In this Example,  I  am just using XML file as a dynamic data source,  for reading data from XML file I  am just using  "XmlTextReader" class. 

The CLR (Common Language Runtime) which specifies the common type libraries to be used by all programming languages on the .NET Platform has full support for XML. The special Namespace reserved for XML is "System.Xml". This namespace has many types to read, write and manipulate XML data. Since the CLR natively supports XML, there is guaranteed ease of use and support of XML in all the languages that will run on .NET platform.. 

The XmlTextReader class has several constructors that allow for various situations, such as reading from an existing stream or from a URL. Most often, perhaps, you will want to read XML from a file, and there's a constructor for this as well.   

XmlTextReader  (a class derived from XmlReader) supports reading XML documents from a text-based stream XmlReader and XMLWriter are the two abstract classes at the core of .NET Framework XML classes. 

XmlTextReader, contained in the .NET Framework's System.XML namespace, reads data from an XML file quickly without placing high demands on system resources. XmlTextReader provides fast, forward-only, non-cached access to XML data. (Forward-only means you can read the XML file from beginning to end but cannot move backwards in the file.)  

XmlValidatingReader is used in conjunction with the XmlTextReader class to provide the capability for DTD, XDR, and XSD schema validation.

When using the NodeType property, it is important to understand how nodes relate to XML elements.  

For example,  In XML file,  <city>auckland</city> 

The XmltextReader class are the following members for reading the data from XML file 

  1. The <city> tag is read as a type XmlNodeType.Element node. The name of the element, "city," is available in the XmlTextReader's Name property.
  2. The "auckland" text data is read as a type XmlNodeType.Text node. The data "auckland" is available in the XmlTextReader's Value property.
  3. The </city> tag is read as a type XmlNodeType.EndElement node. Again, the name of the element, "city," is available in the XmlTextReader's Name property.

Just have a look at the dynamic XML Source file

XML FILE:

<?xml version="1.0" ?>
<weatherinfo>
<
auckland
>
<
updated>31/01/2002 09:00</updated>
<
city>auckland</city>
<
forecast>Fine. A mostly sunny day with light winds</forecast>
<
min>24</min>
<
max>25</max>
</
auckland>
<
chennai>
<
updated>31/01/2002 09:00</updated>
<
city>chennai</city>
<
forecast>Fine.</forecast>
<
min>35</min>
<
max>38</max
>
</
chennai>
<
hongkong>
<
updated>31/01/2002 09:00</updated>
<
city>hongkong</city>
<
forecast>Early rain</forecast>
<
min>23</min>
<
max>25</max
>
</
hongkong>
<
mumbai>
<
updated>31/01/2002 09:00</updated>
<
city>mumbai</city>
<
forecast>Sunny day</forecast>
<
min>29</min>
<
max>35</max
>
</
mumbai>
<
malaysia>
<
updated>31/01/2002 09:00</updated>
<
city>malaysia</city>
<
forecast>raining</forecast>
<
min>24</min>
<
max>25</max
>
</
malaysia>
<
newdelhi>
<
updated>31/01/2002 09:00</updated>
<
city>newdelhi</city>
<
forecast>Fine</forecast>
<
min>30</min>
<
max>35</max
>
</
newdelhi>
<
newyork>
<
updated>31/01/2002 09:00</updated>
<
city>newyork</city>
<
forecast>Very Cold</forecast>
<
min>24</min>
<
max>25</max
>
</
newyork>
<
singapore>
<
updated>31/01/2002 09:00</updated>
<
city>singapore</city>
<
forecast>rain day</forecast>
<
min>31</min>
<
max>35</max
>
</
singapore>
<
tokyo>
<
updated>31/01/2002 09:00</updated>
<
city>tokyo</city>
<
forecast>sunny day </forecast>
<
min>22</min>
<
max>25</max
>
</
tokyo> 
<sydney>
<
updated>31/01/2002 09:00</updated>
<
city>sydney</city>
<
forecast>Fine</forecast>
<
min>21</min>
<
max>25</max
>
</
sydney> 
<washington>
<
updated>31/01/2002 09:00</updated>
<
city>washington</city>
<
forecast>Fine.very cold</forecast>
<
min>23</min>
<
max>25</max
>
</
washington> 
</weatherinfo> 
//XML File End

Source Code

'Source Code Starts
<%@ Page Inherits=" System.Web.UI.MobileControls.MobilePage"Language="vb" %>
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<% @Import Namespace="System.Xml"%> 
<script runat="server">
Public Sub List_ClickEventHandler(ByVal [source] As [Object], ByVal e As ListCommandEventArgs)
Const weatherFileName As String = "c:\weather.xml"
Dim weatherReader As XmlTextReader = Nothing
weatherReader = New XmlTextReader(weatherFileName)
Dim selectedcity As [String] = e.ListItem.Value
While weatherReader.Read()
If weatherReader.NodeType = XmlNodeType.Element Then
If
 weatherReader.Name = e.ListItem.Value Then
WeatherLabel = e.ListItem.Text + ControlChars.Lf + "Weather"
If weatherReader.LocalName.Equals("updated") Then
WeatherLabel = WeatherLabel + ControlChars.Lf + weatherReader.ReadString()
End If
If
 weatherReader.LocalName.Equals("city") Then
WeatherLabel = WeatherLabel + weatherReader.ReadString()
End If
If
 weatherReader.LocalName.Equals("forecast") Then
WeatherLabel = WeatherLabel + weatherReader.ReadString()
End If
If
 weatherReader.LocalName.Equals("min") Then
WeatherLabel = WeatherLabel + "Min Temperature:" + weatherReader.ReadString()
End
 If
If
 weatherReader.LocalName.Equals("max") Then
WeatherLabel = WeatherLabel + "Max Temperature:" + weatherReader.ReadString()
End
 If
End
 If
End
 If
End
 While
ActiveForm = weather
End Sub 'List_ClickEventHandler
</script> 
'List of cities
<mobile:Form runat="server">
<mobile:Label runat="server">Select a City</mobile:Label>
<mobile:List runat="server" id="Listcityvalue"
OnItemCommand="List_ClickEventHandler" >
<item Text="Auckland" Value="auckland" />
<item Text="Chennai" Value="chennai" />
<item Text="Hong Kong" Value="hongkong" />
<item Text="Mumbai" Value="mumbai" />
<item Text="Malaysia" Value="malaysia" />
<item Text="New Delhi" Value="newdelhi" />
<item Text="New York" Value="newyork" />
<item Text="Sydney" Value="sydney" />
<item Text="Singapore" Value="singapore" />
<item Text="Tokyo" Value="tokyo" />
<item Text="Washington" Value="washington" />
</mobile:List>
</mobile:Form> 
<mobile:Form runat="server" id="SecondForm">
<mobile:Label runat="server" id="WelcomeMessage" />
</mobile:Form>
<mobile:Form id="weather" runat = "server">
<mobile:Label runat="server" id="WeatherLabel"/>
</mobile:Form> 
'Source Code End


Similar Articles