Click here to Skip to main content
15,884,605 members
Articles / Web Development / ASP.NET

Ajax Tutorial for Beginners with XML & JSON: Part 2

Rate me:
Please Sign up or sign in to vote.
4.83/5 (38 votes)
3 Jan 2009CPOL5 min read 184.5K   2.4K   121  
This article will give you a brief idea about Ajax with XML & JSON
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Generic;

public partial class FetchWeather : System.Web.UI.Page
{	
    protected void Page_Load(object sender, EventArgs e)
    {
		string type = Request.QueryString["type"];
		string city = Request.QueryString["city"];
		string weatherIcon = "<img src='weathericon.JPG' alt='weather' />";
		List<KeyValuePair<string, string>> list2Ret = new List<KeyValuePair<string, string>>();

		if ( type == null )
			type = "XML";

		string xmlPath = Server.MapPath( "." ) + "\\WeatherDetails.xml";

		XmlDocument doc = new XmlDocument();
		doc.Load( xmlPath );
		XmlNodeList nodeList;		

		if ( type == "XML" )
		{
			nodeList = doc.SelectNodes( "weatherdetails/weatherreport" );

			foreach ( XmlNode node in nodeList )
			{
				list2Ret.Add( new KeyValuePair<string, string>( node.Attributes["city"].InnerText.ToLower(), node.Attributes["weather"].InnerText ) );
			}

			for ( int i = 0; i < list2Ret.Count; i++ )
			{
				if ( list2Ret[i].Key == city.ToLower() )
					Response.Write("<br>Weather Report using XML <br>  "+ list2Ret[i].Value + "<sup>o</sup> C"+ weatherIcon );
			}			
		}
		else if ( type == "JSON" )
		{
			string toJSON = byLibrary.byComponent.XmlToJSON( doc );
			Response.Write( toJSON );
		}		
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions