C# read weather xml

1 /// <summary>
2 /// Msn天气预报
3 /// 涂聚文 2011-02-16 因为被扒手把移动硬盘扒了.重新写.几年的代码因为没有备份,全泡了.重新开始.
4 /// </summary>
5 public class MsnWeatherAPI
6 {
7 /// <summary>
8 /// Msn天气预报
9 /// </summary>
10 /// <param name="wealocations"></param>
11 /// <param name="weadegreetype">c,f</param>
12 /// <returns></returns>
13 public static MsnConditions GetCurrentConditions(string wealocations, string weadegreetype)
14 {
15 string weatherXML = string.Empty;
16 WebClient wc = new WebClient();
17 wc.Encoding = System.Text.Encoding.UTF8;
18 wc.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
19 MsnConditions conditions = new MsnConditions();
20 XmlDocument xmlConditions = new XmlDocument();
21 string url = string.Format("http://weather.msn.com/data.aspx?wealocations=wc:{0}&weadegreetype={1}", wealocations, weadegreetype);
22 byte[] bt = bt = wc.DownloadData(url);
23 wc.Dispose();
24 string data1 = System.Text.Encoding.Default.GetString(bt);
25 xmlConditions.LoadXml(data1);
26 //xmlConditions.Load(url);
27 //XmlNamespaceManager ns = new XmlNamespaceManager(xmlConditions.NameTable);
28 XmlNodeList nodes = xmlConditions.DocumentElement.SelectNodes("/weatherdata/weather");
29
30 //1.可以用
31 #region
32
33 //XmlNodeList elemList = xmlConditions.GetElementsByTagName("weather");
34 //for (int i = 0; i < elemList.Count; i++)
35 //{
36 // conditions.WeatherLocationcode = elemList[i].Attributes["weatherlocationcode"].Value;
37 // onditions.WeatherLocationname = elemList[i].Attributes["weatherlocationname"].Value;
38 //}
39 #endregion
40
41
42 //2.可以用
43 #region
44 //System.Net.WebRequest request = System.Net.WebRequest.Create(url);//创建对weatherURL请求
45 //request.Credentials = System.Net.CredentialCache.DefaultCredentials;//安全设置
46 //System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();//得到响应
47 //if (response.StatusDescription.ToUpper() == "OK")
48 //{
49 // System.IO.Stream weatherStream = response.GetResponseStream();
50 // System.IO.StreamReader read = new System.IO.StreamReader(weatherStream);
51 // weatherXML = read.ReadToEnd();
52 // read.Close();
53 // weatherStream.Close();
54 //}
55 //else
56 //{
57 // weatherXML = string.Empty;
58 //}
59 //response.Close();
60 //
61 //XmlReader reader = XmlReader.Create(new System.IO.StringReader(weatherXML));
62 ////XmlTextReader reader = new XmlTextReader(url);
63
64 //while (reader.Read())
65 //{
66
67 // if (reader.IsStartElement())
68 // {
69 // switch (reader.Name)
70 // {
71 // case "weather":
72 // reader.MoveToAttribute("weatherlocationname");
73 // conditions.WeatherLocationcode = reader.Value;
74 // reader.MoveToAttribute("lat");
75 // conditions.Latitude = reader.Value;
76 // reader.MoveToAttribute("long");
77 // conditions.Longitude = reader.Value;
78 // break;
79
80 // }
81
82 // }
83
84
85 //}
86 #endregion
87 //3.可以用
88 try
89 {
90 if (xmlConditions.SelectSingleNode("/weatherdata/weather") == null)
91 {
92 conditions = null;
93 return conditions;
94 }
95 else
96 {
97 foreach (XmlNode node in nodes)
98 {
99 conditions.WeatherLocationcode = node.Attributes["weatherlocationcode"].Value;
100 conditions.WeatherLocationname = node.Attributes["weatherlocationname"].Value;
101 conditions.ZipCode = node.Attributes["zipcode"].Value;
102 conditions.Latitude = node.Attributes["lat"].Value;
103 conditions.Longitude = node.Attributes["long"].Value;
104 conditions.TimeZone = node.Attributes["timezone"].Value;
105 conditions.EntityId = node.Attributes["entityid"].Value;
106 }
107 return conditions;
108 }
109
110 }
111 catch (Exception ex)
112 {
113 conditions = null;
114 string err = ex.Message.ToString();
115 return conditions;
116 }
117
118 }
119 }
2 /// Msn天气预报
3 /// 涂聚文 2011-02-16 因为被扒手把移动硬盘扒了.重新写.几年的代码因为没有备份,全泡了.重新开始.
4 /// </summary>
5 public class MsnWeatherAPI
6 {
7 /// <summary>
8 /// Msn天气预报
9 /// </summary>
10 /// <param name="wealocations"></param>
11 /// <param name="weadegreetype">c,f</param>
12 /// <returns></returns>
13 public static MsnConditions GetCurrentConditions(string wealocations, string weadegreetype)
14 {
15 string weatherXML = string.Empty;
16 WebClient wc = new WebClient();
17 wc.Encoding = System.Text.Encoding.UTF8;
18 wc.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
19 MsnConditions conditions = new MsnConditions();
20 XmlDocument xmlConditions = new XmlDocument();
21 string url = string.Format("http://weather.msn.com/data.aspx?wealocations=wc:{0}&weadegreetype={1}", wealocations, weadegreetype);
22 byte[] bt = bt = wc.DownloadData(url);
23 wc.Dispose();
24 string data1 = System.Text.Encoding.Default.GetString(bt);
25 xmlConditions.LoadXml(data1);
26 //xmlConditions.Load(url);
27 //XmlNamespaceManager ns = new XmlNamespaceManager(xmlConditions.NameTable);
28 XmlNodeList nodes = xmlConditions.DocumentElement.SelectNodes("/weatherdata/weather");
29
30 //1.可以用
31 #region
32
33 //XmlNodeList elemList = xmlConditions.GetElementsByTagName("weather");
34 //for (int i = 0; i < elemList.Count; i++)
35 //{
36 // conditions.WeatherLocationcode = elemList[i].Attributes["weatherlocationcode"].Value;
37 // onditions.WeatherLocationname = elemList[i].Attributes["weatherlocationname"].Value;
38 //}
39 #endregion
40
41
42 //2.可以用
43 #region
44 //System.Net.WebRequest request = System.Net.WebRequest.Create(url);//创建对weatherURL请求
45 //request.Credentials = System.Net.CredentialCache.DefaultCredentials;//安全设置
46 //System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();//得到响应
47 //if (response.StatusDescription.ToUpper() == "OK")
48 //{
49 // System.IO.Stream weatherStream = response.GetResponseStream();
50 // System.IO.StreamReader read = new System.IO.StreamReader(weatherStream);
51 // weatherXML = read.ReadToEnd();
52 // read.Close();
53 // weatherStream.Close();
54 //}
55 //else
56 //{
57 // weatherXML = string.Empty;
58 //}
59 //response.Close();
60 //
61 //XmlReader reader = XmlReader.Create(new System.IO.StringReader(weatherXML));
62 ////XmlTextReader reader = new XmlTextReader(url);
63
64 //while (reader.Read())
65 //{
66
67 // if (reader.IsStartElement())
68 // {
69 // switch (reader.Name)
70 // {
71 // case "weather":
72 // reader.MoveToAttribute("weatherlocationname");
73 // conditions.WeatherLocationcode = reader.Value;
74 // reader.MoveToAttribute("lat");
75 // conditions.Latitude = reader.Value;
76 // reader.MoveToAttribute("long");
77 // conditions.Longitude = reader.Value;
78 // break;
79
80 // }
81
82 // }
83
84
85 //}
86 #endregion
87 //3.可以用
88 try
89 {
90 if (xmlConditions.SelectSingleNode("/weatherdata/weather") == null)
91 {
92 conditions = null;
93 return conditions;
94 }
95 else
96 {
97 foreach (XmlNode node in nodes)
98 {
99 conditions.WeatherLocationcode = node.Attributes["weatherlocationcode"].Value;
100 conditions.WeatherLocationname = node.Attributes["weatherlocationname"].Value;
101 conditions.ZipCode = node.Attributes["zipcode"].Value;
102 conditions.Latitude = node.Attributes["lat"].Value;
103 conditions.Longitude = node.Attributes["long"].Value;
104 conditions.TimeZone = node.Attributes["timezone"].Value;
105 conditions.EntityId = node.Attributes["entityid"].Value;
106 }
107 return conditions;
108 }
109
110 }
111 catch (Exception ex)
112 {
113 conditions = null;
114 string err = ex.Message.ToString();
115 return conditions;
116 }
117
118 }
119 }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2009-02-16 C# winform combobox 在绑定数据之后插入一项选择项