xml文件格式如下:
文档主要是用来记录中国的传统节日
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<China>
<Festival>
<title>清明</title>
<date>4-5</date>
<contents>中国人纪念先人的日子</contents>
</Festival>
</China>
MVC中Controllers中的XMltestController.cs的代码
//linq to xml单条信息显示
var xDoc = XDocument.Load(Server.MapPath("/Content/jieri.xml"));
var xmlData = from item in xDoc.Descendants("Festival") where item.Element("title").Value == "重阳" select new{title=item.Element("title").Value,date=item.Element("date").Value,contents=item.Element("contents").Value};
jieris je = new jieris();
ViewData["count"] = xmlData.Count();
//必须采用这种循环的结构,否则没有法子显示出来.
foreach (var item in xmlData)
{
je.title = item.title;
je.date = item.date;
je.contents = item.contents;
}
return View(je);
}
/// <summary>
/// linq to xml列表功能
/// </summary>
/// <returns></returns>
public ActionResult List()
{
var xdoc = XElement.Load(Server.MapPath("/Content/jieri.xml"));
//这里的new jieris是定义了一个model类,在后面会把代码放上来,就是三个字段
var list = from items in xdoc.Descendants("Festival") select new jieris { title = items.Element("title").Value, date = items.Element("date").Value, contents = items.Element("contents").Value };
return View(list);
}
/// <summary>
/// linq to xml添加功能
/// </summary>
/// <returns></returns>
public ActionResult add()
{
var xdoc = XElement.Load(Server.MapPath("/Content/jieri.xml"));
//添加
XElement xe = new XElement("Festival",
new XElement("title", "腊八"),
new XElement("date", "12-8"),
new XElement("contents", "农历十二月初八(农历十二月被称为腊月),是我国汉族传统的腊八节,这天我国大多数地区都有吃腊八粥的习俗。"));
xdoc.Add(xe);
xdoc.Save(Server.MapPath("/Content/jieri.xml"));
return RedirectToAction("List");
}
/// <summary>
/// linq to xml修改功能
/// </summary>
/// <returns></returns>
public ActionResult edit(string id)
{
var xDoc = XElement.Load(Server.MapPath("/Content/jieri.xml"));
var updateInfo = from item in xDoc.Descendants("Festival") where item.Element("date").Value == id select item;
foreach (var i in updateInfo)
{
i.Element("title").Value = i.Element("title").Value + "1";
}
xDoc.Save(Server.MapPath("/Content/jieri.xml"));
return RedirectToAction("List");
}
/// <summary>
/// linq to xml删除功能
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Delete(string id)
{
var xDoc = XElement.Load(Server.MapPath("/Content/jieri.xml"));
var deleteinfo = from item in xDoc.Descendants("Festival") where item.Element("date").Value == id select item;
deleteinfo.Remove();
xDoc.Save(Server.MapPath("/Content/jieri.xml"));
return RedirectToAction("List");
}
这里的修改就是很简单的把名字改了一下,没做任何处理,也没有做判断.
这里要说明的一点是,有时候用
var xDoc = XDocument.Load(Server.MapPath("/Content/jieri.xml"));
这种来初始化一个xml,其实这种初始化也是可行的,但是有一点要注意的是,对于列表页面和详细信息页面这个没有问题,可是对于,添加,修改,删除就会出现问题
前台代码
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<En_MVC_test.Models.jieris>>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>List</title>
</head>
<body>
<table>
<tr>
<th></th>
<th>
title
</th>
<th>
date
</th>
<th>
contents
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("修改", "edit", new { id=item.date })%> |
<%= Html.ActionLink("删除", "Delete", new { id=item.date })%>
</td>
<td>
<%= Html.Encode(item.title) %>
</td>
<td>
<%= Html.Encode(item.date) %>
</td>
<td>
<%= Html.Encode(item.contents) %>
</td>
</tr>
<% } %>
</table>
<p>
<%= Html.ActionLink("Create New", "add") %>
</p>
</body>
</html>
model类jieris.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace En_MVC_test.Models
{
public class jieris
{
public string title { get; set; }
public string date { get; set; }
public string contents { get; set; }
}
}
示例2
已知有一个XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>
显示所有数据。
XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl=xn.ChildNodes;
foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
}