Display Local Weather Data On Your Site
page 6 of 7
by Andrew Mooney
Feedback
Average Rating: 
Views (Total / Last 10 Days): 49816/ 103

Putting the Weather on a Web Page

Now that we have all the pieces in place, we need to create a web page that will display our current weather. The web page contains an XML control that will be used to display the current weather conditions. Since the XSL file is static, we add it to the XML control by setting the TransformSource property.

When the web page loads, it attempts to load the NWS XML feed as an XML document and then use that XML document to set the XML control's Document property. If the XML feed is unavailable the default XML file is used to set the DocumentSource property for XML control instead.

I have created an example weather web page which is shown in Listing 4. Name the page Weather.aspx and place it in the root directory of your web application.

Listing 4 - Example of a simple web page that displays the weather

<%@ Page Language="C#" %>
<%@ OutputCache Location="None" VaryByParam="None" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        try
        {
            doc.Load("http://www.weather.gov/data/current_obs/KMDW.xml");
            Xml1.Document = doc;
        }
        catch 
        {
            Xml1.DocumentSource = "App_Data/Weather.xml";
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Local Weather Conditions</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family: Arial, 'Microsoft Sans Serif';font-size: 10pt;">
<asp:Xml ID="Xml1"
 runat="server" TransformSource="App_Data/Weather.xsl"
 EnableViewState="false"></asp:Xml>
</div>
</form>
</body>
</html>

View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-02-26 11:44:13 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search