MVC creates XML and implements addition, deletion, and modification.

Source: Internet
Author: User

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 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.