Esempio n. 1
0
        static void SaveXmlToDB(string xmlFileDir)
        {
            WriteLog("开始保存Xml to DB!");
            DirectoryInfo dir = new DirectoryInfo(xmlFileDir);

            FileInfo[] files = dir.GetFiles("*.xml");
            if (files.Count() == 0)
            {
                WriteLog(string.Format("目录:{0} 下未找到 xml文件!", xmlFileDir));
                return;
            }
            LightNewsModel model      = null;
            string         connString = ConfigurationManager.ConnectionStrings["conStr"].ToString();

            WriteLog(string.Format("共有文件:{0} 个需要导入!", files.Count()));
            string specialTitle;

            foreach (FileInfo file in files)
            {
                model = TranslateXmlToModel(file.FullName, out specialTitle);
                if (model == null)
                {
                    WriteLog(string.Format("屏蔽标题:{0},文件名:{1}", specialTitle, file.Name));
                }
                else
                {
                    SaveNewsPaper(connString, model);
                }
            }

            WriteLog("保存Xml to DB 成功!");
        }
Esempio n. 2
0
        static LightNewsModel TranslateXmlToModel(string filePath, out string outValue)
        {
            StreamReader objReader = new StreamReader(filePath);
            string       fileValue = objReader.ReadToEnd();

            objReader.Close();
            LightNewsModel model = new LightNewsModel();

            fileValue = fileValue.Replace("\r", "").Replace("\n", "");

            model.PublishId = GetValue(fileValue, "PublicId").Trim();

            model.ArticleTitle = GetValue(fileValue, "HeadLine").Trim();
            outValue           = model.ArticleTitle;
            // 屏蔽重复标题:原则:只要标题中出现英文格式的方括号“[ ]”,该标题即舍弃不用
            if (Regex.Match(model.ArticleTitle, @"\[.*?\]").Success)
            {
                return(null);
            }


            model.ReporterName = GetValue(fileValue, "Creator .*?");
            model.ReporterName = GetValue(model.ReporterName, "FullName .*?").Trim();

            model.SourceName = GetValue(fileValue, "NameTopic .*?");
            model.SourceName = GetValue(model.SourceName, "Name").Trim();

            model.ArticleThumb = GetValue(fileValue, "ContentItem xsi:type=\"AppCIType\" .*?");
            model.ArticleThumb = GetValue(model.ArticleThumb, "DataContent").Trim();

            model.ArticleNotes = GetValue(fileValue, "Abstract").Trim();
            model.Keywords     = GetValue(fileValue, "Keyword").Trim();

            string createDate = GetValue(fileValue, "PublishedTime");

            model.CreateDate    = Convert.ToDateTime(createDate);
            model.CreateDateInt = Convert.ToInt32(model.CreateDate.ToString("yyyyMMdd"));

            model.ArticleBody = GetValue(fileValue, "ContentItem xsi:type=\"TextCIType\" .*?");
            model.ArticleBody = GetValue(model.ArticleBody, "DataContent").Trim();

            model.ArticleLength = GetBodyLength(model.ArticleBody);
            return(model);
        }
Esempio n. 3
0
        static void SaveToArticleAd(LightNewsModel model)
        {
            string sql = @"insert into ArticleAd (
                                PublishId,
                                Title,
                                ArticleDate,
                                CreateDate
                            ) values (
                                @PublishId,
                                @Title,
                                @ArticleDate,
                                @CreateDate
                            )";

            object[] sqlParams = new object[]
            {
                model.PublishId,
                model.ArticleTitle,
                model.CreateDate.ToString("yyyy-MM-dd hh:mm:ss"),
                DateTime.Now.ToString("yyyy-MM-dd")
            };
            SQLiteHelper.Instance.ExecuteNonQuery(sql, sqlParams);
        }
Esempio n. 4
0
        static void SaveNewsPaper(string connectionString, LightNewsModel newsModel)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                return;
            }
            try
            {
                if (IsExist(newsModel.PublishId))
                {
                    WriteLog(string.Format("重复数据   - {0} ", newsModel.PublishId));
                    return;
                }


                SqlParameter[] sqlParams = new SqlParameter[]
                {
                    new SqlParameter("@ArticleTitle", newsModel.ArticleTitle),   //标题
                    new SqlParameter("@ReporterName", newsModel.ReporterName),   //作者名
                    new SqlParameter("@SourceName", newsModel.SourceName),       //来源名称
                    new SqlParameter("@ArticleThumb", newsModel.ArticleThumb),   //封面图,可为空
                    new SqlParameter("@ArticleLength", newsModel.ArticleLength), //新闻长度
                    new SqlParameter("@ArticleNotes", newsModel.ArticleNotes),   //编者按或叫摘要的那段文字
                    new SqlParameter("@Keywords", newsModel.Keywords),           //关键字,每个关键字用空格分割
                    new SqlParameter("@CreateDate", newsModel.CreateDate),       //创建时间
                    new SqlParameter("@CreateDateInt", newsModel.CreateDateInt), //创建时间的年月日int型,如20150203
                    new SqlParameter("@ArticleBody", newsModel.ArticleBody)      //文章正文
                };
                // 写入数据库操作,测试时要注释,生产时要放开
                //SqlHelper.ExecteNonQuery(connectionString, System.Data.CommandType.StoredProcedure, "Store_Articles_CreateImport_AEF", sqlParams);
                SaveToArticleAd(newsModel);
            }
            catch (Exception e)
            {
                WriteLog(string.Format("数据库错误: {0}", e.Message));
            }
        }