您好,登錄后才能下訂單哦!
這篇“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>
在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"; } <h3>Index</h3> <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> }
修改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"; } <h3>Index</h3> <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> }
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"; } <h3>Create</h3> @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="創建"/> }
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"; } <h3>Update</h3> @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="修改"/> }
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"; } <h3>Delete</h3> @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文件并實現元素增刪改”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。