经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » XML相关 » XML » 查看文章
ASP.NET?MVC创建XML文件并实现元素增删改
来源:jb51  时间:2022/8/1 9:57:26  对本文有异议

如果创建如下的XML:

  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>
  2. <Students>
  3. <Student Id="1">
  4. <Name>darren</Name>
  5. </Student>
  6. </Students>

创建XML文件

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

  1. public ActionResult Index()
  2. {
  3. return View();
  4. }
  5.  
  6. [HttpPost]
  7. public ActionResult AddXml()
  8. {
  9. string path = Server.MapPath("~/new.xml");
  10. XDocument doc = new XDocument(
  11. new XDeclaration("1.0","utf-8","yes"),
  12. new XElement("Students",new XElement("Student",
  13. new XAttribute("Id","1"),
  14. new XElement("Name","darren")
  15. ))
  16. );
  17. doc.Save(path);
  18. return Json(new {msg = true}, JsonRequestBehavior.AllowGet);
  19. }

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

  1. @model IEnumerable<MvcApplication1.Models.Student>
  2.  
  3. @{
  4. ViewBag.Title = "Index";
  5. Layout = "~/Views/Shared/_Layout.cshtml";
  6. }
  7.  
  8. <h2>Index</h2>
  9.  
  10. <input type="button" value="创建XML" id="create"/>
  11.  
  12. @section scripts
  13. {
  14. <script type="text/javascript">
  15. $(function() {
  16. $('#create').on('click', function() {
  17. $.ajax({
  18. url: '@Url.Action("AddXml", "Home")',
  19. dataType: 'json',
  20. data: {},
  21. type: 'POST',
  22. success: function(data) {
  23. if (data.msg) {
  24. alert('创建成功');
  25. }
  26. }
  27. });
  28. });
  29. });
  30. </script>
  31. }

显示XML文件元素

修改HomeController中的Index方法为:

  1. public ActionResult Index()
  2. {
  3. string path = Server.MapPath("~/new.xml");
  4. List<Student> result = new List<Student>();
  5.  
  6. var nodes = ReadXML(path).Descendants("Student");
  7.  
  8. foreach (var node in nodes)
  9. {
  10. Student student = new Student();
  11. student.Id = Convert.ToInt32(node.Attribute("Id").Value);
  12. foreach (var ele in node.Elements())
  13. {
  14. student.Name = ele.Value;
  15. }
  16. result.Add(student);
  17. }
  18.  
  19. return View(result);
  20. }
  21.  
  22. private XDocument ReadXML(string path)
  23. {
  24. XDocument xDoc = new XDocument();
  25. xDoc = XDocument.Load(path);
  26. return xDoc;
  27. }

修改Home/Index.cshtml为:

  1. @model IEnumerable<MvcApplication1.Models.Student>
  2.  
  3. @{
  4. ViewBag.Title = "Index";
  5. Layout = "~/Views/Shared/_Layout.cshtml";
  6. }
  7.  
  8. <h2>Index</h2>
  9.  
  10. <input type="button" value="创建XML" id="create"/>
  11.  
  12. <table>
  13. <tr>
  14. <th>编号</th>
  15. <th>姓名</th>
  16. </tr>
  17. @foreach (var item in Model)
  18. {
  19. <tr>
  20. <td>@item.Id</td>
  21. <td>@item.Name</td>
  22. <td>@Html.ActionLink("修改","Update","Home",new {id= item.Id},null)</td>
  23. <td>@Html.ActionLink("删除","Delete","Home", new {id = item.Id},null)</td>
  24. </tr>
  25. }
  26. </table>
  27.  
  28. <br/>
  29. @Html.ActionLink("创建","Create","Home")
  30.  
  31. @section scripts
  32. {
  33. <script type="text/javascript">
  34. $(function() {
  35. $('#create').on('click', function() {
  36. $.ajax({
  37. url: '@Url.Action("AddXml", "Home")',
  38. dataType: 'json',
  39. data: {},
  40. type: 'POST',
  41. success: function(data) {
  42. if (data.msg) {
  43. alert('创建成功');
  44. }
  45. }
  46. });
  47. });
  48. });
  49. </script>
  50. }

添加元素到XML文件中

HomeController中:

  1. public ActionResult Create()
  2. {
  3. return View();
  4. }
  5.  
  6. [HttpPost]
  7. public ActionResult Create(Student student)
  8. {
  9. string path = Server.MapPath("~/new.xml");
  10. XDocument xd = XDocument.Load(path);
  11.  
  12. XElement newStudent = new XElement("Student",
  13. new XAttribute("Id", student.Id),
  14. new XElement("Name",student.Name));
  15.  
  16. xd.Root.Add(newStudent);
  17. xd.Save(path);
  18. return RedirectToAction("Index");
  19. }

Home/Create.csthml中:

  1. @model MvcApplication1.Models.Student
  2.  
  3. @{
  4. ViewBag.Title = "Create";
  5. Layout = "~/Views/Shared/_Layout.cshtml";
  6. }
  7.  
  8. <h2>Create</h2>
  9.  
  10. @using (Html.BeginForm("Create", "Home", FormMethod.Post, new {id = "addForm"}))
  11. {
  12. @Html.LabelFor(m => m.Id)
  13. @Html.EditorFor(m => m.Id)
  14.  
  15. <br/>
  16. @Html.LabelFor(m => m.Name)
  17. @Html.EditorFor(m => m.Name)
  18.  
  19. <br/>
  20. <input type="submit" value="创建"/>
  21. }

修改XML文件中的元素

HomeController中:

  1. public ActionResult Update(string id)
  2. {
  3. string path = Server.MapPath("~/new.xml");
  4. XElement xe = XElement.Load(path);
  5. var studentXe = xe.Elements("Student").Where(e => e.Attribute("Id").Value == id).FirstOrDefault();
  6.  
  7. Student student = new Student();
  8. student.Id = Convert.ToInt32(studentXe.Attribute("Id").Value);
  9. student.Name = studentXe.Element("Name").Value;
  10. return View(student);
  11. }
  12.  
  13. [HttpPost]
  14. public ActionResult Update(Student student)
  15. {
  16. string path = Server.MapPath("~/new.xml");
  17. var studentId = student.Id.ToString();
  18. XDocument xd = XDocument.Load(path);
  19. XElement node =
  20. xd.Root.Elements("Student").Where(e => e.Attribute("Id").Value == studentId).FirstOrDefault();
  21. node.SetElementValue("Name", student.Name);
  22. xd.Save(path);
  23. return RedirectToAction("Index");
  24. }

Home/Update.csthml中:

  1. @model MvcApplication1.Models.Student
  2.  
  3. @{
  4. ViewBag.Title = "Update";
  5. Layout = "~/Views/Shared/_Layout.cshtml";
  6. }
  7.  
  8. <h2>Update</h2>
  9.  
  10. @using (Html.BeginForm("Update", "Home", FormMethod.Post, new {id = "editForm"}))
  11. {
  12. @Html.HiddenFor(m => m.Id)
  13. @Html.LabelFor(m => m.Name)
  14. @Html.EditorFor(m => m.Name)
  15.  
  16. <br/>
  17. <input type="submit" value="修改"/>
  18. }

删除XML文件中的元素

HomeController中:

  1. public ActionResult Delete(string id)
  2. {
  3. string path = Server.MapPath("~/new.xml");
  4. XElement xe = XElement.Load(path);
  5. var studentXe = xe.Elements("Student").Where(e => e.Attribute("Id").Value == id).FirstOrDefault();
  6.  
  7. Student student = new Student();
  8. student.Id = Convert.ToInt32(studentXe.Attribute("Id").Value);
  9. student.Name = studentXe.Element("Name").Value;
  10. return View(student);
  11. }
  12.  
  13. [HttpPost]
  14. public ActionResult Delete(Student student)
  15. {
  16. string path = Server.MapPath("~/new.xml");
  17. var studentId = student.Id.ToString();
  18. XDocument xd = XDocument.Load(path);
  19. xd.Root.Elements("Student").Where(e => e.Attribute("Id").Value == studentId).Remove();
  20. xd.Save(path);
  21. return RedirectToAction("Index");
  22. }

Home/Delete.cshtml中:

  1. @model MvcApplication1.Models.Student
  2.  
  3. @{
  4. ViewBag.Title = "Delete";
  5. Layout = "~/Views/Shared/_Layout.cshtml";
  6. }
  7.  
  8. <h2>Delete</h2>
  9.  
  10. @Model.Id
  11. <br/>
  12. @Model.Name
  13. <br/>
  14.  
  15. @using (Html.BeginForm("Delete", "Home", FormMethod.Post, new {id = "delForm"}))
  16. {
  17. @Html.HiddenFor(m => m.Id)
  18. <input type="submit" value="删除"/>
  19. }

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号