コード例 #1
0
 public WeatherAlarmViewModel(WeatherAlarm alarm)
     : this(alarm.Title, alarm.Text, alarm.Level)
 {
 }
コード例 #2
0
        //[System.Web.Services.WebMethod]
        public void AlarmWearher()
        {
            List <WeatherAlarm> WeatherAlarmList = new List <WeatherAlarm>();
            List <hazards>      hazardsListData  = new List <hazards>();
            string         webAddr        = "http://opendata.cwb.gov.tw/opendata/MFC/W-C0033-001.xml";
            HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(webAddr);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "GET";
            HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();

            StreamReader stmReader = new StreamReader(response.GetResponseStream());

            string stringResult = stmReader.ReadToEnd();

            XmlDocument xml = new XmlDocument();

            xml.LoadXml(stringResult);

            XmlNamespaceManager xnm = new XmlNamespaceManager(xml.NameTable);

            xnm.AddNamespace("x", "urn:cwb:gov:tw:cwbcommon:0.1");

            //xmlDoc.Root.Descendants().Attributes().Where(x => x.IsNamespaceDeclaration).Remove();
            string xpath = "/x:cwbopendata/x:dataset/x:location";

            foreach (XmlNode xn in xml.SelectNodes(xpath, xnm))
            {
                hazards hazardsInformationItem = new hazards();
                var     wc = xn.ChildNodes;//(ypath, xnm);
                foreach (XmlNode ChileNodeInfo in wc)
                {
                    if (ChileNodeInfo.Name == "locationName")
                    {
                        hazardsInformationItem.locationName = ChileNodeInfo.InnerText;
                    }
                    else if (ChileNodeInfo.Name == "hazardConditions")
                    {
                        hazardConditions hazardConditionsItems = new hazardConditions();
                        foreach (XmlNode ChileNodeInfoElemat in ChileNodeInfo)
                        {
                            if (ChileNodeInfoElemat.Name == "hazards")
                            {
                                foreach (XmlNode ChileNodeInfoTimeData in ChileNodeInfoElemat)
                                {
                                    if (ChileNodeInfoTimeData.Name == "info")
                                    {
                                        hazardinfo hazardinfoItem = new hazardinfo();
                                        foreach (XmlNode ChileNodeInfoTimeDatainfo in ChileNodeInfoTimeData)
                                        {
                                            if (ChileNodeInfoTimeDatainfo.Name == "phenomena")
                                            {
                                                hazardinfoItem.phenomena = ChileNodeInfoTimeDatainfo.InnerText;
                                            }
                                            else if (ChileNodeInfoTimeDatainfo.Name == "significance")
                                            {
                                                hazardinfoItem.significance = ChileNodeInfoTimeDatainfo.InnerText;
                                            }
                                        }
                                        hazardConditionsItems.info = hazardinfoItem;
                                    }
                                    else if (ChileNodeInfoTimeData.Name == "validTime")
                                    {
                                        hazardvalidTime hazardvalidTimeItem = new hazardvalidTime();
                                        foreach (XmlNode ChileNodeInfoTimeDatainfo in ChileNodeInfoTimeData)
                                        {
                                            if (ChileNodeInfoTimeDatainfo.Name == "startTime")
                                            {
                                                hazardvalidTimeItem.startTime = ChileNodeInfoTimeDatainfo.InnerText;
                                            }
                                            else if (ChileNodeInfoTimeDatainfo.Name == "endTime")
                                            {
                                                hazardvalidTimeItem.endTime = ChileNodeInfoTimeDatainfo.InnerText;
                                            }
                                        }
                                        hazardConditionsItems.validTime = hazardvalidTimeItem;
                                    }
                                    else if (ChileNodeInfoTimeData.Name == "hazard")
                                    {
                                        hazardshazard hazardshazardItem = new hazardshazard();
                                        foreach (XmlNode ChileNodeInfoTimeDatainfo in ChileNodeInfoTimeData)
                                        {
                                            if (ChileNodeInfoTimeDatainfo.Name == "info")
                                            {
                                                foreach (XmlNode ChileNodeInfoTimeDatainfoItem in ChileNodeInfoTimeDatainfo)
                                                {
                                                    if (ChileNodeInfoTimeDatainfoItem.Name == "phenomena")
                                                    {
                                                        hazardshazardItem.phenomena = ChileNodeInfoTimeDatainfoItem.InnerText;
                                                    }
                                                    else if (ChileNodeInfoTimeDatainfoItem.Name == "affectedAreas")
                                                    {
                                                        foreach (XmlNode ChileNodeInfoTimeDatainfoItem1 in ChileNodeInfoTimeDatainfoItem)
                                                        {
                                                            if (ChileNodeInfoTimeDatainfoItem1.Name == "location")
                                                            {
                                                                foreach (XmlNode ChileNodeInfoTimeDatainfoItem2 in ChileNodeInfoTimeDatainfoItem1)
                                                                {
                                                                    if (ChileNodeInfoTimeDatainfoItem2.Name == "locationName")
                                                                    {
                                                                        hazardshazardItem.affectedAreas.Add(ChileNodeInfoTimeDatainfoItem2.InnerText);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        hazardConditionsItems.hazard = hazardshazardItem;
                                    }
                                }
                            }
                        }
                        hazardsInformationItem.hazardConditionsData = hazardConditionsItems;
                    }
                }
                hazardsListData.Add(hazardsInformationItem);
            }
            //ListBoxAlarm.Items.Clear();
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[2] {
                new DataColumn("Location", typeof(string)), new DataColumn("Summary", typeof(string))
            });
            foreach (hazards item in hazardsListData)
            {
                WeatherAlarm newWeatherAlarm = new WeatherAlarm();
                ListItem     newListItem     = new ListItem();
                string       HazardsInfo     = item.locationName;

                string SummaryText = "";
                //if (string.IsNullOrEmpty(item.hazardConditionsData.hazard.phenomena) == false)
                if (item.hazardConditionsData.validTime != null)
                {
                    SummaryText = item.hazardConditionsData.info.phenomena + item.hazardConditionsData.info.significance + " ";
                    if (item.hazardConditionsData.hazard != null)
                    {
                        HazardsInfo += item.hazardConditionsData.hazard.phenomena + "(";
                        foreach (string items in item.hazardConditionsData.hazard.affectedAreas)
                        {
                            HazardsInfo += items + ",";
                        }
                        HazardsInfo += ")";
                    }
                }
                newWeatherAlarm.Location = HazardsInfo;
                newWeatherAlarm.Summary  = SummaryText;
                WeatherAlarmList.Add(newWeatherAlarm);
                dt.Rows.Add(HazardsInfo, SummaryText);
                //ListBoxAlarm.Items.Add(HazardsInfo);
            }

            //string json = Newtonsoft.Json.JsonConvert.SerializeObject(WeatherAlarmList);
            //return json;
            ListBoxAlarm.DataSource = dt;
            ListBoxAlarm.DataBind();
        }