博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC创建XML,并实现增删改
阅读量:5745 次
发布时间:2019-06-18

本文共 5820 字,大约阅读时间需要 19 分钟。

原文:

如果创建如下的XML:

darren

 

  创建XML文件

在HomeController中,在根目录下创建new.xml文件:

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);        }

 

在Index.cshtml中通过异步请求:

@model IEnumerable
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml";}

Index

@section scripts{
}

  显示XML文件元素

修改HomeController中的Index方法为:

public ActionResult Index()        {            string path = Server.MapPath("~/new.xml");            List
result = new List
(); 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; }
       
修改Home/Index.cshtml为:
@model IEnumerable
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml";}

Index

@foreach (var item in Model) {
}
编号 姓名
@item.Id @item.Name @Html.ActionLink("修改","Update","Home",new {id= item.Id},null) @Html.ActionLink("删除","Delete","Home", new {id = item.Id},null)
@Html.ActionLink("创建","Create","Home")@section scripts{
}

 

  添加元素到XML文件中

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");        }

 

Home/Create.csthml中:

@model MvcApplication1.Models.Student@{    ViewBag.Title = "Create";    Layout = "~/Views/Shared/_Layout.cshtml";}

Create

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new {id = "addForm"})){ @Html.LabelFor(m => m.Id) @Html.EditorFor(m => m.Id)
@Html.LabelFor(m => m.Name) @Html.EditorFor(m => m.Name)
}

 

  修改XML文件中的元素

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");        }

 

Home/Update.csthml中:

@model MvcApplication1.Models.Student@{    ViewBag.Title = "Update";    Layout = "~/Views/Shared/_Layout.cshtml";}

Update

@using (Html.BeginForm("Update", "Home", FormMethod.Post, new {id = "editForm"})){ @Html.HiddenFor(m => m.Id) @Html.LabelFor(m => m.Name) @Html.EditorFor(m => m.Name)
}

 

  删除XML文件中的元素

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");        }

 

Home/Delete.cshtml中:

@model MvcApplication1.Models.Student@{    ViewBag.Title = "Delete";    Layout = "~/Views/Shared/_Layout.cshtml";}

Delete

@Model.Id
@Model.Name
@using (Html.BeginForm("Delete", "Home", FormMethod.Post, new {id = "delForm"})){ @Html.HiddenFor(m => m.Id) }

 

转载地址:http://mjxzx.baihongyu.com/

你可能感兴趣的文章
李彦宏:百度需要从这七点开始改变!
查看>>
关于QTableWidgetItem::setItemPrototype的理解
查看>>
IOS 手势之左右滑动
查看>>
Redis实现分布式锁的几种方案
查看>>
一文详解Linux系统常用监控工具
查看>>
我的友情链接
查看>>
Window上python开发--4.Django的用户登录模块User
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Linux实用逻辑卷之建立LVM
查看>>
我的友情链接
查看>>
无法录制下拉菜单的解决方法
查看>>
Liunx下搭建简单的DHCP服务器
查看>>
CoreImage 之 CIDetector iOS 人脸识别
查看>>
EditText插入QQ表情源码
查看>>
ASP.NET core 搭建于 Deepin 2015.4 记录
查看>>
斐讯 K3C V32.1.45.267 V1.1官改升级操作
查看>>
Krpano 全景生成-droplet
查看>>
Krpano 开启多边形编辑模式
查看>>
无法登录域
查看>>