標籤:
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);//顯示子節點點文本
}
}
c# mvc如何擷取xml檔案