ASP.NET?MVC創(chuàng)建XML文件并實(shí)現(xiàn)元素增刪改
如果創(chuàng)建如下的XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Students>
<Student Id="1">
<Name>darren</Name>
</Student>
</Students>創(chuàng)建XML文件
在HomeController中,在根目錄下創(chuàng)建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="創(chuàng)建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('創(chuàng)建成功');
}
}
});
});
});
</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="創(chuàng)建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("創(chuàng)建","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('創(chuàng)建成功');
}
}
});
});
});
</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="創(chuàng)建"/>
}修改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="刪除"/>
}到此這篇關(guān)于ASP.NET MVC創(chuàng)建XML文件并實(shí)現(xiàn)元素增刪改的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net中GridView和DataGrid相同列合并實(shí)現(xiàn)代碼
asp.net中GridView和DataGrid相同列合并實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-10-10
asp.net?core實(shí)體類生產(chǎn)CRUD后臺管理界面
這篇文章主要為大家介紹了asp.net?core實(shí)體類生產(chǎn)CRUD后臺管理界面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Visual Studio 2017正式版發(fā)布 Mac版新功能特性有哪些
Visual Studio 2017正式版推出時(shí)間什么時(shí)候?Mac版新功能特性又有哪些?這篇文章就為大家詳細(xì)介紹Visual Studio 2017正式版的最新消息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
WEB在模態(tài)窗體里導(dǎo)出或下載文件功能代碼
實(shí)現(xiàn)在模態(tài)窗體里導(dǎo)出或下載文件,具體功能代碼如下,感興趣的朋友可以參考下哈2013-06-06
asp.net 獲取TreeView中第一個(gè)選中的節(jié)點(diǎn)
今天做的項(xiàng)目中有一個(gè)要獲取TreeView中第一個(gè)選中的節(jié)點(diǎn),當(dāng)然子節(jié)點(diǎn)己包含checkbox以前做過,用的時(shí)候又不知道怎么做了,花了點(diǎn)時(shí)間又寫了一下,記錄下來,以備下次用.2010-03-03
Asp.net core實(shí)現(xiàn)PushStream視頻流推送
這篇文章介紹了Asp.net core實(shí)現(xiàn)PushStream視頻流推送的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
將文件上傳、下載(以二進(jìn)制流保存到數(shù)據(jù)庫)實(shí)現(xiàn)代碼
將文件以二進(jìn)制流的格式寫入數(shù)據(jù)庫:首先獲得文件路徑,然后將文件以二進(jìn)制讀出保存在一個(gè)二進(jìn)制數(shù)組中具體請祥看本文,希望對你有所幫助2013-05-05

