Esempio n. 1
0
        public static void start()
        {
            using (Model_info_type2 db = new Model_info_type2())
            {
                List <Information> infos = new List <Information>();
                try
                {
                    infos = GetInformation.GetRates <Information>("http://www.nationalbank.kz/rss/rates.xml");
                    infos.Sort();
                }
                catch (Exception)
                {
                    MailSender.Send_mail("XML файл не соделрит нужну информацию или содержет её в неподдерживаемом виде ");
                }

                List <Information> test = new List <Information>();
                try
                {
                    test = db.information.ToList();
                }
                catch (Exception ex)
                {
                    MailSender.Send_mail(ex.ToString());
                }


                Console.WriteLine("Проверка данных");
                if (!Equals(infos, test) || Convert.ToDateTime(test[0].pubDate) < DateTime.Now)
                {
                    Console.WriteLine("Обновление данных");
                    db.Database.ExecuteSqlCommand("delete from Information");
                    db.information.AddRange(infos);
                    db.Database.CreateIfNotExists();
                    db.SaveChanges();
                }
                Console.WriteLine("Данные совпадают");



                //db.info.Add(infos[0]);
                //db.SaveChanges();
            }
        }
Esempio n. 2
0
        public static List <T> GetRates <T>(string xml)
        {
            List <T> rates = new List <T>();


            XDocument xDoc = new XDocument();

            try
            {
                xDoc = XDocument.Load(xml);
            }
            catch (Exception)
            {
                MailSender.Send_mail("Невозможно получить XML файл");
            }
            try
            {
                foreach (var item in xDoc.Descendants("item"))
                {
                    Object istanance = Activator.CreateInstance(typeof(T));
                    foreach (var items in item.Elements())
                    {
                        foreach (PropertyInfo prop in istanance.GetType().GetProperties())
                        {
                            if (items.Name == prop.Name)
                            {
                                istanance.GetType().GetProperty(prop.Name).SetValue(istanance, items.Value);
                            }
                        }
                    }
                    rates.Add((T)Convert.ChangeType(istanance, typeof(T)));
                }
            }
            catch (Exception)
            {
                MailSender.Send_mail("Невозможно создать объект на основе XML,возможно,поменялась конструкция XML файла");
            }


            Console.WriteLine(rates.Count());
            return(rates);
        }