public ActionResult Edit(News News) { News dbNews = new News { Source = LevelType.Local, SourceLink = CurrentProfile.AssociationID, Distribution = LevelType.Local, DistributionLink = CurrentProfile.AssociationID, Publish = DateTime.Now, AuthorID = CurrentProfile.PersonID }; if (News.NewsId != Guid.Empty) dbNews = reposetory.GetNewsItem(News.NewsId); if (dbNews == null) return HttpNotFound(); if (ModelState.IsValid) { dbNews.Headline = News.Headline; dbNews.Teaser = News.Teaser; dbNews.Internal = News.Internal; dbNews.Content = News.Content; dbNews.Publish = News.Publish; dbNews.Depublish = News.Depublish; dbNews.AuthorID = CurrentProfile.PersonID; dbNews.Trim(); if (reposetory.Save(dbNews)) { News.NewsId = dbNews.NewsId; ModelState.Clear(); ViewBag.FormSucces = true; } } return View(News); }
public ActionResult _NewsMigration(Guid? id, string url) { if (string.IsNullOrWhiteSpace(url)) return null; int newsId; string Result = ""; string[] Urls = url.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); foreach (string cururl in Urls) { string theUrl; if (int.TryParse(cururl, out newsId)) { theUrl = string.Format(@"http://www.natteravnene.dk/default.asp?page_id=12&news_id={0}", newsId.ToString()); } else { theUrl = cururl; if (!theUrl.ToLower().StartsWith("http://www.")) continue; } HtmlWeb web = new HtmlWeb { AutoDetectEncoding = false, OverrideEncoding = Encoding.GetEncoding("iso-8859-1"), }; //text/html; charset=iso-8859-1 HtmlDocument doc = web.Load(theUrl); var nodes = doc.DocumentNode.SelectNodes("//td[@class='frontleft']"); if (nodes != null && nodes.Count == 1) { //List<HtmlNode> images = new List<HtmlNode>(); News news = new News { Publish = DateTime.Now.Date.AddHours(DateTime.Now.Hour), Source = LevelType.National, Distribution = LevelType.National, AuthorID = CurrentProfile.PersonID, Teaser = "", Content = "" }; if (id != null && id != Guid.Empty) { news.Source = LevelType.Local; news.SourceLink = (Guid)id; news.Distribution = LevelType.Local; news.DistributionLink = (Guid)id; } DateTime Date; var node = nodes.First(); news.Headline = node.SelectSingleNode("./h2").InnerText; node.SelectSingleNode("./h2").Remove(); HtmlNode DateNode = node.ChildNodes[4]; if (DateTime.TryParse(DateNode.InnerText, out Date)) { news.Publish = Date; } if (node.ChildNodes[0].Name == "#text") node.ChildNodes[0].Remove(); if (node.ChildNodes[0].Name == "p") node.ChildNodes[0].Remove(); if (node.ChildNodes[0].Name == "#text") node.ChildNodes[0].Remove(); if (node.ChildNodes[0].Name == "br") node.ChildNodes[0].Remove(); if (node.ChildNodes[0].Name == "#text") node.ChildNodes[0].Remove(); if (node.ChildNodes[0].Name == "br") node.ChildNodes[0].Remove(); if (node.ChildNodes[0].Name == "br") node.ChildNodes[0].Remove(); news.Content = node.InnerHtml; foreach (HtmlNode teser in node.ChildNodes) { if (!string.IsNullOrWhiteSpace(teser.InnerText)) { news.Teaser = HtmlEntity.DeEntitize(teser.InnerText); break; } } news.Trim(); if (!string.IsNullOrWhiteSpace(news.Content) && reposetory.Save(news)) { int num = 0; var imgNodes = node.SelectNodes(".//img/@src"); if (imgNodes != null) { foreach (HtmlNode img in imgNodes) { string src = img.Attributes["src"].Value; if (!src.StartsWith("http://")) { //img.Attributes["src"].Value = "http://www.natteravnene.dk/" + src; img.Attributes["src"].Value = SaveImageFile("http://www.natteravnene.dk/" + src, news.NewsId, num); if (node.Attributes["width"] != null) img.Attributes["width"].Remove(); if (node.Attributes["height"] != null) img.Attributes["height"].Remove(); img.Attributes.Add("class", "img-responsive"); num++; } //images.Add(img); //.Attributes["src"].Value); } } news.Content = node.InnerHtml; if (reposetory.Save(news)) { Result += "<hr />" + node.InnerHtml; } } } } return Json(new { success = true, data = Result }); }