public static void ConvertXmlAndStoreInDatabase(XDocument xmlFile) { // converts downloaded xml data & add to database newsObjectsList.Clear(); using (SQLiteConnection conn = new SQLiteConnection(DBLocation)) { conn.DropTable <NewsObject>(); conn.CreateTable <NewsObject>(); foreach (var item in xmlFile.Descendants("event")) { // get date & time values from XML (string) string dateOnly = item.Element("date").Value.TrimEnd(); string timeOnly = item.Element("time").Value.TrimEnd(); // convert date & time strings to DateTime object DateTime tempDateTime = Convert_Strings_DateAndTime_To_SingleDateTimeObject(dateOnly, timeOnly, cultureInfo); // adjust time for GMT in xml file & add 1 hour if it is currently daylight saving bool isDaylightSavingsTime = tempDateTime.IsDaylightSavingTime(); //Log.Debug("DEBUG", "\n\n\n It is currently daylightsavings time: " // + isDaylightSavingsTime.ToString() + "\n\n\n"); if (isDaylightSavingsTime == true) { // add 1 hour to every datetime to bring GMT time to correct time during daylight savings time tempDateTime = tempDateTime.AddHours(1); // NOTE: tempDateTime.AddHours(1); (doesn't work because) // This method does not change the value of this DateTime. // Instead, it returns a new DateTime whose value is the result of this operation. (MSDN) } // set alarm to go off a set amount before news alart time tempDateTime = tempDateTime.AddMinutes(TimeToGoOffBeforeMarketAnnouncement); // convert DateTime object to a long of ticks long dateTimeInTicks = tempDateTime.Ticks; // create a newsObject for every 'event' in xml file NewsObject newsObject = new NewsObject { Title = item.Element("title").Value.TrimEnd(), // .Value - removes surrounding tags CountryChar = item.Element("country").Value.TrimEnd(), MarketImpact = item.Element("impact").Value.TrimEnd(), DateInTicks = dateTimeInTicks }; // insert newsObject into database conn.Insert(newsObject); } ; } }
// method to convert newsObject to userAlert object public static UserAlert ConvertNewsObjectToUserAlert(NewsObject newsObject) { UserAlert userAlert = new UserAlert { // don't assign ID - SQLite will do this automatically when object is inserted into DB Title = newsObject.Title, CountryChar = newsObject.CountryChar, MarketImpact = newsObject.MarketImpact, DateAndTime = newsObject.DateAndTime, DateInTicks = newsObject.DateInTicks, IsPersonalAlert = false, // because this is converting a market event DescriptionOfPersonalEvent = string.Empty }; return(userAlert); }