Exemple #1
0
        public List <Tides> XmlToTide(string xml, string locale)
        {
            Tides        tide   = new Tides();
            List <Tides> list   = new List <Tides>();
            XmlDocument  xmldoc = new XmlDocument();

            xmldoc.LoadXml(xml);
            string time;

            string[] timeParts;
            string   date;
            DateTime dt = new DateTime();


            XmlNodeList xmlelem = xmldoc.GetElementsByTagName("pr");

            foreach (XmlNode t in xmlelem)
            {
                tide = new Tides();

                date = t.Attributes["t"].Value;


                //format the time correctly
                time      = date.Substring(date.Length - 5);
                timeParts = time.Split(':');
                if (Int32.Parse(timeParts[0]) > 12)
                {
                    tide.Time = Int32.Parse(timeParts[0]) - 12 + ":" + timeParts[1] + " pm";
                }
                else
                {
                    tide.Time = time + " am";
                }
                double cen;
                double feet;
                //get the day from the date
                dt       = DateTime.ParseExact(date.Substring(0, 10), "yyyy-MM-dd", CultureInfo.InvariantCulture);
                tide.Day = dt.ToString("dddd");

                tide.Date = date.Substring(0, 10).Replace('-', '/');

                cen      = double.Parse(t.Attributes["v"].Value) * 100;
                tide.Cen = cen.ToString();

                feet      = cen * 0.0328084;
                tide.Feet = feet.ToString();

                tide.Locale = locale;
                tide.HL     = t.Attributes["type"].Value;

                tide.Index = 1;

                list.Add(tide);
            }
            return(list);
        }
Exemple #2
0
        public static List <Tides> ConvertTides(Stream file)
        {
            Tides        tide  = new Tides();
            List <Tides> tides = new List <Tides>();
            string       date;

            using (XmlReader reader = XmlReader.Create(file))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        switch (reader.Name)
                        {
                        case "item":
                            tide = new Tides();
                            break;

                        case "date":
                            if (reader.Read() && tide != null)
                            {
                                date       = reader.Value.Trim();
                                tide.Date  = date;
                                tide.Index = Int32.Parse(date.Substring(5, 2));
                            }
                            break;

                        case "day":
                            if (reader.Read() && tide != null)
                            {
                                tide.Day = reader.Value.Trim();
                            }
                            break;

                        case "time":
                            if (reader.Read() && tide != null)
                            {
                                tide.Time = reader.Value.Trim();
                            }
                            break;

                        case "pred_in_ft":
                            if (reader.Read() && tide != null)
                            {
                                tide.Feet = reader.Value.Trim();
                            }
                            break;

                        case "pred_in_cm":
                            reader.Read();
                            tide.Cen = reader.Value.Trim();
                            break;

                        case "highlow":
                            if (reader.Read() && tide != null)
                            {
                                tide.HL = reader.Value.Trim();
                            }
                            break;
                        }
                    }
                    else
                    {
                        if (reader.Name == "item")
                        {
                            tides.Add(tide); tide = null;
                        }
                    }
                }
            }
            tides.Sort((x, y) => x.Index.CompareTo(y.Index));
            return(tides);
        }