Google API 天气数据缓存到一个XML中

 1 //通过Google API 获取天气数据 并读取存入 XML中 
2
3 private void CreateXML()
4 {
5 XmlNodeList weekList = GetWeatherXML();
6 string strXml = "<weather></weather>";
7 doc.LoadXml(strXml);
8
9 foreach (var obj in weekList)
10 {
11 XmlNode xmlNode = obj as XmlNode;
12 XmlElement newElem = doc.CreateElement("forecast_conditions");
13 doc.DocumentElement.AppendChild(newElem);
14 XmlElement weekElem = doc.CreateElement("day_of_week");
15 weekElem.InnerText = xmlNode.SelectSingleNode("day_of_week").Attributes["data"].InnerText;
16 doc.LastChild.LastChild.AppendChild(weekElem);
17 XmlElement lowElem = doc.CreateElement("low");
18 lowElem.InnerText = xmlNode.SelectSingleNode("low").Attributes["data"].InnerText;
19 doc.LastChild.LastChild.AppendChild(lowElem);
20 XmlElement highElem = doc.CreateElement("high");
21 highElem.InnerText = xmlNode.SelectSingleNode("high").Attributes["data"].InnerText;
22 doc.LastChild.LastChild.AppendChild(highElem);
23 XmlElement iconElem = doc.CreateElement("icon");
24 iconElem.InnerText = xmlNode.SelectSingleNode("icon").Attributes["data"].InnerText;
25 doc.LastChild.LastChild.AppendChild(iconElem);
26 XmlElement conditionElem = doc.CreateElement("condition");
27 conditionElem.InnerText = xmlNode.SelectSingleNode("condition").Attributes["data"].InnerText;
28 doc.LastChild.LastChild.AppendChild(conditionElem);
29 }
30
31 doc.PreserveWhitespace = true;
32 doc.Save("data.xml");
33 }
34
35 //在APP.Config中添加AppSetting key 方便替换Google API的访问地址的修改
36
37 <configuration>
38 <appSettings>
39 <add key="googleAPI" value="http://www.google.com.hk/ig/api?hl=zh-cn#weather="/>
40 </appSettings>
41 </configuration>
42
43 //通过API 获取谷歌的天气数据信息
44
45 public XmlNodeList GetWeatherXML()
46 {
47 string con = ConfigurationManager.AppSettings["googleAPI"].ToString();
48 string googleAPI = con.Replace("#", "&").ToString();
49 XmlNodeList weekList = null;
50 try
51 {
52 XmlDocument xmlDocument = GoogleWeatherAPI_ParserXML(@googleAPI);
53 weekList = xmlDocument.SelectNodes("xml_api_reply/weather/forecast_conditions");
54
55
56 }
57 catch
58 {
59 } return weekList;
60 }
61
62 //解析XML 通过Google API 获取天气数据
63
64 private XmlDocument GoogleWeatherAPI_ParserXML(string baseUrl)
65 {
66 HttpWebRequest GWP_Request;
67 HttpWebResponse GWP_Response = null;
68 XmlDocument GWP_XMLdoc = null;
69 try
70 {
71 GWP_Request = (HttpWebRequest)WebRequest.Create(string.Format(baseUrl));
72 GWP_Request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4";
73 GWP_Response = (HttpWebResponse)GWP_Request.GetResponse();
74 GWP_XMLdoc = new XmlDocument();
75 GWP_XMLdoc.Load(GWP_Response.GetResponseStream());
76 }
77 catch (Exception ex)
78 {
79 Console.WriteLine(ex.Message);
80 }
81 GWP_Response.Close();
82 return GWP_XMLdoc;
83 }
84
85 //更新XML中的数据信息
86
87 private void UpdateXML()
88 {
89 string strXmlPath = Application.StartupPath + "\\data.xml";
90 doc.Load(strXmlPath);
91 xe = doc.FirstChild as XmlElement;
92 XmlNode xnRC = xe.LastChild.LastChild.SelectSingleNode("day_of_week");
93 if (xnRC != null)
94 {
95 xnRC.InnerText = "星期八";
96 doc.Save(strXmlPath);
97 }
98 }

 

posted @ 2012-03-09 11:05  都市夜归人  阅读(194)  评论(0)    收藏  举报