当前位置: 代码迷 >> ASP.NET >> 英文版天气预报,该如何解决
  详细解决方案

英文版天气预报,该如何解决

热度:3065   发布时间:2013-02-25 00:00:00.0
英文版天气预报
有谁做过没有,做过的把代码帖出来看看啊

------解决方案--------------------------------------------------------
没做过.UP
------解决方案--------------------------------------------------------
顶一下吧
------解决方案--------------------------------------------------------
参考下问天网啦
------解决方案--------------------------------------------------------
C# code
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");            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)),                    FToC(int.Parse(node.Attributes["high"].InnerText)));                Console.WriteLine("-----------------------------------------------------------");            }                                }        private static string FToC(int f)        {            return Math.Round((f - 32) / 1.8, 1).ToString();        }        public static Stream DownloadWebPage(Uri url)        {            try            {                using (WebClient client = new WebClient())                {                    client.UseDefaultCredentials = true;                    client.Headers.Add(System.Net.HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;)");                    return client.OpenRead(url);                    //using (StreamReader reader = new StreamReader(client.OpenRead(url)))                    //{                      //  return reader.ReadToEnd();                    //}                }            }            catch (WebException)            {                return null;            }        }    }}