public static WeatherForecast GetForecast(string area) { WeatherForecast f = new WeatherForecast(); try { string json = GetJson(area, "forecast"); if (!json.Equals(string.Empty) && !json.Contains("error data")) { XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json), XmlDictionaryReaderQuotas.Max); XmlDocument xdoc = new XmlDocument(); xdoc.Load(reader); Log.WriteDebug("GetForecast>xdoc>>>>>" + xdoc.InnerText); #region 输出完整的XML,分析格式 //StringWriter stringWriter = new StringWriter(); //XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter); //xmlTextWriter.Formatting = Formatting.Indented; //foreach (XmlNode xmlNode in xdoc) //{ // xmlNode.WriteTo(xmlTextWriter); //} //Log.WriteLog(stringWriter.ToString()); #endregion #region 用xml解析json,并为相关的成员变量赋值 ///获取到的属性信息 XmlNode info = xdoc.FirstChild.SelectSingleNode("c"); f.AreaID = info.SelectSingleNode("c1").InnerText; f.ENName = info.SelectSingleNode("c2").InnerText; f.CNName = info.SelectSingleNode("c3").InnerText; f.ENCityName = info.SelectSingleNode("c4").InnerText; f.CNCityName = info.SelectSingleNode("c5").InnerText; f.ENProvName = info.SelectSingleNode("c6").InnerText; f.CNProvName = info.SelectSingleNode("c7").InnerText; f.ENCountryName = info.SelectSingleNode("c8").InnerText; f.CNCountryName = info.SelectSingleNode("c9").InnerText; f.CityLevel = uint.Parse(info.SelectSingleNode("c10").InnerText); f.CityAreaCode = info.SelectSingleNode("c11").InnerText; f.PostCode = uint.Parse(info.SelectSingleNode("c12").InnerText); f.Longitude = float.Parse(info.SelectSingleNode("c13").InnerText); f.Latitude = float.Parse(info.SelectSingleNode("c14").InnerText); f.Altitude = int.Parse(info.SelectSingleNode("c15").InnerText); f.RadarStationNo = info.SelectSingleNode("c16").InnerText; ///f0 数据发布时间 IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true); f.PublishTime = System.DateTime.ParseExact(xdoc.FirstChild.SelectSingleNode("f").SelectSingleNode("f0").FirstChild.InnerText, "yyyyMMddHHmm", culture); ///第一天 XmlNode DayOne = xdoc.FirstChild.SelectSingleNode("f").SelectSingleNode("f1").FirstChild; f.DayWeatherCodeOne = DayOne.SelectSingleNode("fa").InnerText; f.NightWeatherCodeOne = DayOne.SelectSingleNode("fb").InnerText; f.DayTemperatureOne = DayOne.SelectSingleNode("fc").InnerText; f.NightTemperatureOne = DayOne.SelectSingleNode("fd").InnerText; f.DayWindDirectionOne = DayOne.SelectSingleNode("fe").InnerText; f.NightWindDirectionOne = DayOne.SelectSingleNode("ff").InnerText; f.DayWindForceOne = DayOne.SelectSingleNode("fg").InnerText; f.NightWindForceOne = DayOne.SelectSingleNode("fh").InnerText; f.SunriseTimeOne = DayOne.SelectSingleNode("fi").InnerText.Split('|')[0]; f.SunsetTimeOne = DayOne.SelectSingleNode("fi").InnerText.Split('|')[1]; ///第二天 XmlNode DayTwo = DayOne.NextSibling; f.DayWeatherCodeTwo = DayTwo.SelectSingleNode("fa").InnerText; f.NightWeatherCodeTwo = DayTwo.SelectSingleNode("fb").InnerText; f.DayTemperatureTwo = DayTwo.SelectSingleNode("fc").InnerText; f.NightTemperatureTwo = DayTwo.SelectSingleNode("fd").InnerText; f.DayWindDirectionTwo = DayTwo.SelectSingleNode("fe").InnerText; f.NightWindDirectionTwo = DayTwo.SelectSingleNode("ff").InnerText; f.DayWindForceTwo = DayTwo.SelectSingleNode("fg").InnerText; f.NightWindForceTwo = DayTwo.SelectSingleNode("fh").InnerText; f.SunriseTimeTwo = DayTwo.SelectSingleNode("fi").InnerText.Split('|')[0]; f.SunsetTimeTwo = DayTwo.SelectSingleNode("fi").InnerText.Split('|')[1]; ///第三天 XmlNode DayThree = DayTwo.NextSibling; f.DayWeatherCodeThree = DayThree.SelectSingleNode("fa").InnerText; f.NightWeatherCodeThree = DayThree.SelectSingleNode("fb").InnerText; f.DayTemperatureThree = DayThree.SelectSingleNode("fc").InnerText; f.NightTemperatureThree = DayThree.SelectSingleNode("fd").InnerText; f.DayWindDirectionThree = DayThree.SelectSingleNode("fe").InnerText; f.NightWindDirectionThree = DayThree.SelectSingleNode("ff").InnerText; f.DayWindForceThree = DayThree.SelectSingleNode("fg").InnerText; f.NightWindForceThree = DayThree.SelectSingleNode("fh").InnerText; f.SunriseTimeThree = DayThree.SelectSingleNode("fi").InnerText.Split('|')[0]; f.SunsetTimeThree = DayThree.SelectSingleNode("fi").InnerText.Split('|')[1]; #endregion } } catch (Exception ex) { Log.WriteError("GetForecast错误>>>>>" + ex.Message); } return(f); }
public static WeatherForecast GetForecast(string area) { WeatherForecast f = new WeatherForecast(); try { string json = GetJson(area, "forecast"); if (!json.Equals(string.Empty) && !json.Contains("error data")) { XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json), XmlDictionaryReaderQuotas.Max); XmlDocument xdoc = new XmlDocument(); xdoc.Load(reader); Log.WriteDebug("GetForecast>xdoc>>>>>" + xdoc.InnerText); #region 输出完整的XML,分析格式 //StringWriter stringWriter = new StringWriter(); //XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter); //xmlTextWriter.Formatting = Formatting.Indented; //foreach (XmlNode xmlNode in xdoc) //{ // xmlNode.WriteTo(xmlTextWriter); //} //Log.WriteLog(stringWriter.ToString()); #endregion #region 用xml解析json,并为相关的成员变量赋值 ///获取到的属性信息 XmlNode info = xdoc.FirstChild.SelectSingleNode("c"); f.AreaID = info.SelectSingleNode("c1").InnerText; f.ENName = info.SelectSingleNode("c2").InnerText; f.CNName = info.SelectSingleNode("c3").InnerText; f.ENCityName = info.SelectSingleNode("c4").InnerText; f.CNCityName = info.SelectSingleNode("c5").InnerText; f.ENProvName = info.SelectSingleNode("c6").InnerText; f.CNProvName = info.SelectSingleNode("c7").InnerText; f.ENCountryName = info.SelectSingleNode("c8").InnerText; f.CNCountryName = info.SelectSingleNode("c9").InnerText; f.CityLevel = uint.Parse(info.SelectSingleNode("c10").InnerText); f.CityAreaCode = info.SelectSingleNode("c11").InnerText; f.PostCode = uint.Parse(info.SelectSingleNode("c12").InnerText); f.Longitude = float.Parse(info.SelectSingleNode("c13").InnerText); f.Latitude = float.Parse(info.SelectSingleNode("c14").InnerText); f.Altitude = int.Parse(info.SelectSingleNode("c15").InnerText); f.RadarStationNo = info.SelectSingleNode("c16").InnerText; ///f0 数据发布时间 IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true); f.PublishTime = System.DateTime.ParseExact(xdoc.FirstChild.SelectSingleNode("f").SelectSingleNode("f0").FirstChild.InnerText, "yyyyMMddHHmm", culture); ///第一天 XmlNode DayOne = xdoc.FirstChild.SelectSingleNode("f").SelectSingleNode("f1").FirstChild; f.DayWeatherCodeOne = DayOne.SelectSingleNode("fa").InnerText; f.NightWeatherCodeOne = DayOne.SelectSingleNode("fb").InnerText; f.DayTemperatureOne = DayOne.SelectSingleNode("fc").InnerText; f.NightTemperatureOne = DayOne.SelectSingleNode("fd").InnerText; f.DayWindDirectionOne = DayOne.SelectSingleNode("fe").InnerText; f.NightWindDirectionOne = DayOne.SelectSingleNode("ff").InnerText; f.DayWindForceOne = DayOne.SelectSingleNode("fg").InnerText; f.NightWindForceOne = DayOne.SelectSingleNode("fh").InnerText; f.SunriseTimeOne = DayOne.SelectSingleNode("fi").InnerText.Split('|')[0]; f.SunsetTimeOne = DayOne.SelectSingleNode("fi").InnerText.Split('|')[1]; ///第二天 XmlNode DayTwo = DayOne.NextSibling; f.DayWeatherCodeTwo = DayTwo.SelectSingleNode("fa").InnerText; f.NightWeatherCodeTwo = DayTwo.SelectSingleNode("fb").InnerText; f.DayTemperatureTwo = DayTwo.SelectSingleNode("fc").InnerText; f.NightTemperatureTwo = DayTwo.SelectSingleNode("fd").InnerText; f.DayWindDirectionTwo = DayTwo.SelectSingleNode("fe").InnerText; f.NightWindDirectionTwo = DayTwo.SelectSingleNode("ff").InnerText; f.DayWindForceTwo = DayTwo.SelectSingleNode("fg").InnerText; f.NightWindForceTwo = DayTwo.SelectSingleNode("fh").InnerText; f.SunriseTimeTwo = DayTwo.SelectSingleNode("fi").InnerText.Split('|')[0]; f.SunsetTimeTwo = DayTwo.SelectSingleNode("fi").InnerText.Split('|')[1]; ///第三天 XmlNode DayThree = DayTwo.NextSibling; f.DayWeatherCodeThree = DayThree.SelectSingleNode("fa").InnerText; f.NightWeatherCodeThree = DayThree.SelectSingleNode("fb").InnerText; f.DayTemperatureThree = DayThree.SelectSingleNode("fc").InnerText; f.NightTemperatureThree = DayThree.SelectSingleNode("fd").InnerText; f.DayWindDirectionThree = DayThree.SelectSingleNode("fe").InnerText; f.NightWindDirectionThree = DayThree.SelectSingleNode("ff").InnerText; f.DayWindForceThree = DayThree.SelectSingleNode("fg").InnerText; f.NightWindForceThree = DayThree.SelectSingleNode("fh").InnerText; f.SunriseTimeThree = DayThree.SelectSingleNode("fi").InnerText.Split('|')[0]; f.SunsetTimeThree = DayThree.SelectSingleNode("fi").InnerText.Split('|')[1]; #endregion } } catch (Exception ex) { Log.WriteError("GetForecast错误>>>>>" + ex.Message); } return f; }
public static List <Dictionary <string, string> > GetForecastInfo(string area) { List <Dictionary <string, string> > result = new List <Dictionary <string, string> >(); try { WeatherForecast f = GetForecast(area); if (f.CNCityName != string.Empty) { Dictionary <string, string> dict = new Dictionary <string, string>(); dict["Title"] = f.CNCityName + "天气预报 " + DateTime.Now.ToString("yyyy年M月d日"); dict["Description"] = ""; dict["PicUrl"] = ""; dict["Url"] = ""; result.Add(dict); dict = new Dictionary <string, string>(); dict["Title"] = string.Format("{0}市今天天气情况如下:{1}日出,白天{2},{3}摄氏度,{4}{5};{6}日落,晚上{7},{8}摄氏度,{9}{10}。--发布时间[{11}]", f.CNCityName, f.SunriseTimeOne, WeatherForecast.GetWeatherCNByCode(f.DayWeatherCodeOne), f.DayTemperatureOne, WeatherForecast.GetWindDirectionCNByCode(f.DayWindDirectionOne), WeatherForecast.GetWindForceCNByCode(f.DayWindForceOne), f.SunsetTimeOne, WeatherForecast.GetWeatherCNByCode(f.NightWeatherCodeOne), f.NightTemperatureOne, WeatherForecast.GetWindDirectionCNByCode(f.NightWindDirectionOne), WeatherForecast.GetWindForceCNByCode(f.NightWindForceOne), f.PublishTime.ToString("yyyy年MM月dd日-HH:mm:ss")).Replace(" ", ""); dict["Description"] = ""; if (int.Parse(DateTime.Now.ToString("HH")) < 6 || int.Parse(DateTime.Now.ToString("HH")) > 18) { dict["PicUrl"] = string.Format("http://demonlue.wicp.net/111/Images/Weather/Night/{0}.png", f.NightWeatherCodeOne); //Log.WriteLog("晚上"); } else //(int.Parse(DateTime.Now.ToString("HH")) > 6 && int.Parse(DateTime.Now.ToString("HH")) < 18) { dict["PicUrl"] = string.Format("http://demonlue.wicp.net/111/Images/Weather/Day/{0}.png", f.DayWeatherCodeOne); //Log.WriteLog("白天"); } Log.WriteLog(dict["PicUrl"]); dict["Url"] = ""; result.Add(dict); } return(result); } catch (Exception ex) { Log.WriteDebug(ex.Message); return(result); } }