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

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

如果创建如下的XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <Students> <Student Id="1"> <Name>darren</Name> </Student> </Students> 创建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<MvcApplication1.Models.Student> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <input type="button" value="创建XML" id="create"/> @section scripts { <script type="text/javascript"> $(function() { $('#create').on('click', function() { $.ajax({ url: '@Url.Action("AddXml", "Home")', dataType: 'json', data: {}, type: 'POST', success: function(data) { if (data.msg) { alert('创建成功'); } } }); }); }); </script> } 显示XML文件元素

修改HomeController中的Index方法为:

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

修改Home/Index.cshtml为:

@model IEnumerable<MvcApplication1.Models.Student> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <input type="button" value="创建XML" id="create"/> <table> <tr> <th>编号</th> <th>姓名</th> </tr> @foreach (var item in Model) { <tr> <td>@item.Id</td> <td>@item.Name</td> <td>@Html.ActionLink("修改","Update","Home",new {id= item.Id},null)</td> <td>@Html.ActionLink("删除","Delete","Home", new {id = item.Id},null)</td> </tr> } </table> <br/> @Html.ActionLink("创建","Create","Home") @section scripts { <script type="text/javascript"> $(function() { $('#create').on('click', function() { $.ajax({ url: '@Url.Action("AddXml", "Home")', dataType: 'json', data: {}, type: 'POST', success: function(data) { if (data.msg) { alert('创建成功'); } } }); }); }); </script> } 添加元素到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"; } <h2>Create</h2> @using (Html.BeginForm("Create", "Home", FormMethod.Post, new {id = "addForm"})) { @Html.LabelFor(m => m.Id) @Html.EditorFor(m => m.Id) <br/> @Html.LabelFor(m => m.Name) @Html.EditorFor(m => m.Name) <br/> <input type="submit" value="创建"/> } 修改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"; } <h2>Update</h2> @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) <br/> <input type="submit" value="修改"/> } 删除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"; } <h2>Delete</h2> @Model.Id <br/> @Model.Name <br/> @using (Html.BeginForm("Delete", "Home", FormMethod.Post, new {id = "delForm"})) { @Html.HiddenFor(m => m.Id) <input type="submit" value="删除"/> }

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

推荐阅读

    XML引入的常见问题

    XML引入的常见问题,,作者:angelgavin来源:CSDN 一般问题 什么是XML 可扩展标记语言(XML)是一个基于Web的通用语言,它使开发人员能够从许多不

    asp中文图片验证码的实现代码

    asp中文图片验证码的实现代码,,这个代码是在别人的增加对汉字的基础功能,谢谢你,谢谢轻烟。 以前的图片验证代码很容易破解,所以在目前的基础

    1.Netty 概述

    1.Netty 概述,协议,高性能,原生 NIO 存在的问题NIO 的类库和 API 繁杂, 使用麻烦: 需要熟练掌握 Selector, ServerSocketChannel, SocketC

    在wps导入xml|WPS表格导入XML数据

    在wps导入xml|WPS表格导入XML数据,导入,如何在,wps,xml,1.WPS表格怎么导入XML数据可以有三种方式导入数据:直接打开数据文件,使用ODBC数据源

    ctnet需要打开吗|CTNET什么意思

    ctnet需要打开吗|CTNET什么意思,,1. ctnet需要打开吗1、打开手机设置,选择网络和连接,点击移动网络。2、选择接入点名称(APN)进入之后选择中国

    联通uninet设置|联通接入点uninet

    联通uninet设置|联通接入点uninet,,1. 联通接入点uninet设置如下,手机不同,对应的菜单不同,楼主要自己找找。:第一步:在菜单(功能表)-工具-设置-