If you create the following XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?><Students> <Student Id="1"> <Name>darren</Name> </Student></Students>
Create an XML file
In HomeController, create the new. xml file in the root directory:
public ActionResult Index() { return View(); } [HttpPost] public ActionResult AddXml() { string path = Server.MapPath("~/new.xml"); XDocument doc = new XDocument( new XDeclaration("1.0","utf-8","yes"), new XElement("Students",new XElement("Student", new XAttribute("Id","1"), new XElement("Name","darren") )) ); doc.Save(path); return Json(new {msg = true}, JsonRequestBehavior.AllowGet); }
Use asynchronous requests in Index. cshtml:
@ Model IEnumerable <MvcApplication1.Models. Student> @ {ViewBag. Title = "Index"; Layout = "~ /Views/Shared/_ Layout. cshtml ";}
Show XML file elements
Modify the Index method in HomeController as follows:
public ActionResult Index() { string path = Server.MapPath("~/new.xml"); List<Student> result = new List<Student>(); var nodes = ReadXML(path).Descendants("Student"); foreach (var node in nodes) { Student student = new Student(); student.Id = Convert.ToInt32(node.Attribute("Id").Value); foreach (var ele in node.Elements()) { student.Name = ele.Value; } result.Add(student); } return View(result); } private XDocument ReadXML(string path) { XDocument xDoc = new XDocument(); xDoc = XDocument.Load(path); return xDoc; }
Modify Home/Index. cshtml:
@ Model IEnumerable <MvcApplication1.Models. Student> @ {ViewBag. Title = "Index"; Layout = "~ /Views/Shared/_ Layout. cshtml ";}
Add element to XML file
HomeController:
public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Student student) { string path = Server.MapPath("~/new.xml"); XDocument xd = XDocument.Load(path); XElement newStudent = new XElement("Student", new XAttribute("Id", student.Id), new XElement("Name",student.Name)); xd.Root.Add(newStudent); xd.Save(path); return RedirectToAction("Index"); }
In Home/Create. csthml:
@ Model MvcApplication1.Models. Student @ {ViewBag. Title = "Create"; Layout = "~ /Views/Shared/_ Layout. cshtml ";}< h2> Create
Modify elements in an XML file
HomeController:
public ActionResult Update(string id) { string path = Server.MapPath("~/new.xml"); XElement xe = XElement.Load(path); var studentXe = xe.Elements("Student").Where(e => e.Attribute("Id").Value == id).FirstOrDefault(); Student student = new Student(); student.Id = Convert.ToInt32(studentXe.Attribute("Id").Value); student.Name = studentXe.Element("Name").Value; return View(student); } [HttpPost] public ActionResult Update(Student student) { string path = Server.MapPath("~/new.xml"); var studentId = student.Id.ToString(); XDocument xd = XDocument.Load(path); XElement node = xd.Root.Elements("Student").Where(e => e.Attribute("Id").Value == studentId).FirstOrDefault(); node.SetElementValue("Name", student.Name); xd.Save(path); return RedirectToAction("Index"); }
In Home/Update. csthml:
@ Model MvcApplication1.Models. Student @ {ViewBag. Title = "Update"; Layout = "~ /Views/Shared/_ Layout. cshtml ";}< h2> Update
Delete elements in an XML file
HomeController:
public ActionResult Delete(string id) { string path = Server.MapPath("~/new.xml"); XElement xe = XElement.Load(path); var studentXe = xe.Elements("Student").Where(e => e.Attribute("Id").Value == id).FirstOrDefault(); Student student = new Student(); student.Id = Convert.ToInt32(studentXe.Attribute("Id").Value); student.Name = studentXe.Element("Name").Value; return View(student); } [HttpPost] public ActionResult Delete(Student student) { string path = Server.MapPath("~/new.xml"); var studentId = student.Id.ToString(); XDocument xd = XDocument.Load(path); xd.Root.Elements("Student").Where(e => e.Attribute("Id").Value == studentId).Remove(); xd.Save(path); return RedirectToAction("Index"); }
In Home/Delete. cshtml:
@ Model MvcApplication1.Models. Student @ {ViewBag. Title = "Delete"; Layout = "~ /Views/Shared/_ Layout. cshtml ";}< h2> Delete