static NewspaperMP GetNewsPaperMP(DateTime curDate, string xmlPath) { try { if (checkUpdateTimer != null && DateTime.Now.Hour > 20) { checkUpdateTimer.Dispose(); log.Info("已过晚8点,不在等待数据!" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); Console.WriteLine("已过晚8点,不在等待数据,5秒后 程序自动退出。"); System.Threading.Thread.Sleep(5 * 1000); Environment.Exit(0); } else { NewspaperMP mp = XmlManager.ReadXmlToModel(xmlPath, log); return(mp); } } catch (Exception e) { log.Error("读出Xml时出错:" + e); if (e.Message.Contains("404")) { log.Info(string.Format("Xml 文档未生成,下次重试:{0}", DateTime.Now.AddMinutes(30).ToString("yyyy-MM-dd hh:mm:ss"))); Console.WriteLine(string.Format("Ipad数据未生成,请勿关闭窗口,下次重试:{0}", DateTime.Now.AddMinutes(30).ToString("yyyy-MM-dd hh:mm:ss"))); GetTimerStart(curDate); Console.ReadLine(); } } return(null); }
static void ImportData(DateTime curDate) { string dateStr = curDate.ToString("yyyyMMdd"); log.Info("开始导入数据!日期:" + dateStr); Console.WriteLine("开始导入数据:" + dateStr); var dw = curDate.DayOfWeek; if (dw == DayOfWeek.Saturday || dw == DayOfWeek.Sunday) { log.Info(string.Format("日期{1}, {0}不执行。", dateStr, dw.ToString())); Console.WriteLine(string.Format("日期{1}, {0}不执行。", dateStr, dw.ToString())); } else { string xmlPath = string.Format(ConfigurationManager.AppSettings["XMLUrl"], dateStr); //NewspaperMP mp = XmlManager.ReadXmlToModel(xmlPath, log); NewspaperMP mp = GetNewsPaperMP(curDate, xmlPath); SaveMPToDB(mp); AddNewsEvent(dateStr); } log.Info("导入数据结束!日期:" + dateStr); if (checkUpdateTimer != null) { checkUpdateTimer.Dispose(); Console.WriteLine("导入数据结束!5秒后 程序自动退出。"); //Console.ReadLine(); System.Threading.Thread.Sleep(5 * 1000); Environment.Exit(0); } }
static void SaveMPToDB(NewspaperMP mp) { var connection1 = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"]; var connection2 = System.Configuration.ConfigurationManager.ConnectionStrings["conStr2"]; string connectionString = connection1 == null ? string.Empty : connection1.ToString().Trim(); string connectionString2 = connection2 == null ? string.Empty : connection2.ToString().Trim(); if (mp == null || mp.Newspapers.Count == 0) { log.Info("XML文件为空或不包含任何一个版面!"); Console.WriteLine("XML文件为空或不包含任何一个版面!"); return; } log.Info("开始导入数据, 版面数:" + mp.Newspapers.Count); Console.WriteLine("开始导入数据, 版面数:" + mp.Newspapers.Count); foreach (var newspaper in mp.Newspapers) { SaveNewsPaper(connectionString, newspaper); SaveNewsPaper2(connectionString2, newspaper); } }
public static NewspaperMP ReadXmlToModel(string url, ILog log) { NewspaperMP newspaperMP = null; //XmlReader xmlReader = null; //try //{ WebRequest wReq = WebRequest.Create(url); Stream respStream = wReq.GetResponse().GetResponseStream(); //FileStream respStream = new FileStream("D:\\dycjrb_20150112.xml",FileMode.Open,FileAccess.Read); log.Info("读取网络XML文件成功!Url:" + url); using (StreamReader sr = new StreamReader(respStream, Encoding.GetEncoding("utf-8"))) { byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sr.ReadToEnd()); sr.Close(); // 反序列化 log.Info("开始反序列化数据..."); XmlSerializer serializer = new XmlSerializer(typeof(NewspaperMP)); newspaperMP = (NewspaperMP)serializer.Deserialize(new MemoryStream(buffer)); log.Info("反序列化数据成功!"); return(newspaperMP); } //} //catch (Exception e) //{ // log.Error("读出Xml时出错:" + e); // if (e.Message.Contains("404")) // { // log.Error(string.Format("Xml 文档未生成,下次重试:{0}", DateTime.Now.AddMinutes(30).ToString("yyyy-MM-dd hh:mm:ss"))); // } //} //return null; }