日期:2014-05-18 浏览次数:20517 次
using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Net; using System.IO; namespace Weather { class Program { static void Main(string[] args) { if (args.Length == 0) { DisplayWeather("CHXX0049"); DisplayInfo(); } else { switch (args[0].ToLower()) { case "hongkong": case "hk": DisplayWeather("CHXX0049"); break; case "bj": case "beijing": DisplayWeather("CHXX0008"); break; case "gz": case "guangzhou": DisplayWeather("CHXX0037"); break; case "sz": case "shengzhen": DisplayWeather("CHXX0120"); break; case "sh": case "shanghai": DisplayWeather("CHXX0116"); break; default: DisplayWeather(args[0]); break; }//end of switch } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } private static void DisplayInfo() { Console.WriteLine(); Console.WriteLine("**************************"); Console.WriteLine("Weather HongKong -Hong Kong"); Console.WriteLine("Weather Beijing -Beijing"); Console.WriteLine("Weather Shanghai -Shanghai"); Console.WriteLine("Weather Guangzhou -Guangzhou"); Console.WriteLine("Weather Shenzhen -Shenzhen"); Console.WriteLine("Weather [city code] -visit details: http://weather.yahoo.com/China/CHXX/regional.html"); Console.WriteLine(); } private static void DisplayWeather(string cityCode) { XmlDocument document = new XmlDocument(); Uri url = new Uri("http://xml.weather.yahoo.com/forecastrss?p="+cityCode ); //string source = DownloadWebPage(url); try { document.Load(DownloadWebPage(url)); } catch (Exception ex) { Console.WriteLine(ex.Message); } XmlNode n=document.SelectSingleNode("/rss/channel/title"); Console.WriteLine(n.InnerText); Console.WriteLine("***********************************************************"); XmlNodeList nodes = document.GetElementsByTagName("forecast", @"http://xml.weather.yahoo.com/ns/rss/1.0"); foreach (XmlNode node in nodes) { Console.WriteLine("Date: {0} {1}\nWeather: {2}\nTemperature: {3}C to {4}C", node.Attributes["date"].InnerText, node.Attributes["day"].InnerText, node.Attributes["text"].InnerText, FToC(int.Parse(node.Attributes["low"].InnerText)),