ASP.NET MVC创建XML文件并实现元素增删改

这篇文章介绍了ASP.NET MVC创建XML文件并实现元素增删改的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

如果创建如下的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) }

到此这篇关于ASP.NET MVC创建XML文件并实现元素增删改的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持0133技术站。

以上就是ASP.NET MVC创建XML文件并实现元素增删改的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程