首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

语音播报实时天候 的实现

2013-11-15 
语音播报实时天气 的实现//Get weather data(xml format) string weather webClient.DownloadString(str

语音播报实时天气 的实现
//Get weather data(xml format)
string weather = webClient.DownloadString(string.Format(
"http://php.weather.sina.com.cn/xml.php?city={0}&password=DJOYnieT8234jlsK&day=0",
HttpUtility.UrlEncode(json["city"], Encoding.GetEncoding("GB2312"))));
//Console.WriteLine(weather);
var xml = new XmlDocument();
xml.LoadXml(weather);

这次取到的天气信息就是XML格式的了,也很方便。但需要注意的是此,构建URL的时候要把城市采用GB2312格式编码,WebClient需要指定UTF-8格式。天气信息取到了,下面就是编字符串,让它说话了,这里附上全部的代码,总共23行:

1 //Initialize Speaker
2 var reader = new SpeechSynthesizer();
3 reader.Speak("I'm a programer,Hello, World! ");
4
5 var webClient = new WebClient() { Encoding = Encoding.UTF8 };
6 //Get location city
7 var location = webClient.DownloadString("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json");
8 var json = new JavaScriptSerializer().Deserialize<dynamic>(location);
9 //Read city from utf-8 format
10 var city = HttpUtility.UrlDecode(json["city"]);
11 //Get weather data(xml format)
12 string weather = webClient.DownloadString(string.Format(
13 "http://php.weather.sina.com.cn/xml.php?city={0}&password=DJOYnieT8234jlsK&day=0",
14 HttpUtility.UrlEncode(json["city"], Encoding.GetEncoding("GB2312"))));
15 //Console.WriteLine(weather);
16 var xml = new XmlDocument();
17 xml.LoadXml(weather);
18 //Get weather detail
19 var root = xml.SelectSingleNode("/Profiles/Weather");
20 var detail = root["status1"].InnerText + "," + root["direction1"].InnerText
21 + root["power1"].InnerText.Replace("-", "到") + "级,"
22 + root["gm_s"].InnerText + root["yd_s"].InnerText;
23 reader.SpeakAsync("今天是" + DateTime.Now.ToShortDateString() + "," + city + "" + detail);

?

热点排行