说实话,此次看见linq to xml主要是为了在芒果里操作文件,只是单位的机器太烂了,没法子试,只能拿MVC来试了.废话不多说,上代码.
xml文件格式如下:
文档主要是用来记录中国的传统节日
<?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,其实这种初始化也是可行的,但是有一点要注意的是,对于列表页面和详细信息页面这个没有问题,可是对于,添加,修改,删除就会出现问题.为什么呢.大家就去看一下MSDN吧.
前台代码
<%@ 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>
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; } } }