網站首頁 編程語言 正文
如果創建如下的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="刪除"/>
}
原文鏈接:https://www.cnblogs.com/darrenji/p/3790250.html
- 上一篇:C#實現對象的序列化和反序列化_C#教程
- 下一篇:C#自定義集合初始化器_C#教程
相關推薦
- 2023-06-04 Pandas.DataFrame時間序列數據處理的實現_python
- 2022-09-22 Mybaits一級緩存和二級緩存分別是什么,區別是什么?
- 2022-02-23 MAC清除所有的mobileprovision
- 2022-07-14 Python實現印章代碼的算法解析_python
- 2022-04-02 ?Python錯誤與異常處理_python
- 2022-06-07 使用Docker容器部署rocketmq單機的全過程_docker
- 2022-07-23 C語言實例實現二叉搜索樹詳解_C 語言
- 2022-06-23 python基礎之while循環、for循環詳解及舉例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支