zoukankan      html  css  js  c++  java
  • 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 阅读笔记
    抽象和接口
    Appium+python自动化2-启动百度app
    Appium+python自动化1-环境搭建
    python之图像识别
    python之栈和队列
    python之语音识别(speech模块)
    使用Python计算IP、TCP、UDP校验和
  • 原文地址:https://www.cnblogs.com/geovindu/p/1956373.html
Copyright © 2011-2022 走看看