public List<Testimonial> GetAll() { List<Testimonial> testimonialList = new List<Testimonial>(); string testimonialFileName = "testimonials.xml"; string testimonialFilePath = HttpContext.Current.Server.MapPath(Path.Combine("~/App_Data", testimonialFileName)); XElement xElement = XElement.Load(testimonialFilePath); IEnumerable<XElement> allTestimonials = xElement.Elements(); IEnumerable<XElement> visibleTestimonials = from t in allTestimonials where t.Attribute("IsVisible").Value == "true" select t; foreach (XElement visibleTestimonial in visibleTestimonials) { Testimonial testimonial = new Testimonial(); testimonial.Name = visibleTestimonial.Element("Name").Value; testimonial.Email = visibleTestimonial.Element("Email").Value; testimonial.Country = visibleTestimonial.Element("Country").Value; testimonial.Message = visibleTestimonial.Element("Message").Value; testimonial.CreatedDate = DateTime.Parse(visibleTestimonial.Attribute("CreatedDate").Value); testimonialList.Add(testimonial); } return testimonialList; }
public ActionResult Testimonials(Testimonial model) { if (!ModelState.IsValid) return View(model); model.IsVisible = true; model.CreatedDate = DateTime.UtcNow; Testimonial testimonial = repo.Add(model); TempData["status"] = testimonial != null ? true : false; return RedirectToAction("Testimonials"); }
private void WriteToXml(Testimonial testimonial) { string testimonialFileName = "testimonials.xml"; string testimonialFilePath = HttpContext.Current.Server.MapPath(Path.Combine("~/App_Data", testimonialFileName)); XElement xElement = XElement.Load(testimonialFilePath); xElement.Add(new XElement("Testimonial", new XAttribute("IsVisible", testimonial.IsVisible), new XAttribute("CreatedDate", testimonial.CreatedDate.ToString("dd-MM-yyyy")), new XElement("Name", testimonial.Name), new XElement("Email", testimonial.Email), new XElement("Country", testimonial.Country), new XElement("Message", testimonial.Message))); xElement.Save(testimonialFilePath); }
public Testimonial Add(Testimonial testimonial) { WriteToXml(testimonial); return testimonial; }