c# mvc如何获取xml文件

超、凢脫俗 2021-12-21 06:33 312阅读 0赞

xml文件格式如下:
文档主要是用来记录中国的传统节日

[html] view plaincopy
<?xml version=”1.0” encoding=”utf-8”?>



清明
4-5
中国人纪念先人的日子

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);
}

///


/// linq to xml列表功能
///

///
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);
}

///


/// linq to xml添加功能
///

///
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”);
}

///


/// linq to xml修改功能
///

///
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”);
}

///


/// linq to xml删除功能
///

///
///
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>” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>



List










<% foreach (var item in Model) { %>








<% } %>


title

date

contents

<%= Html.ActionLink(“修改”, “edit”, new { id=item.date })%> |
<%= Html.ActionLink(“删除”, “Delete”, new { id=item.date })%>

<%= Html.Encode(item.title) %>

<%= Html.Encode(item.date) %>

<%= Html.Encode(item.contents) %>



<%= Html.ActionLink(“Create New”, “add”) %>




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

  1. 已知有一个XML文件(bookstore.xml)如下:
  2. <?xml version="1.0" encoding="gb2312"?>
  3. <bookstore>
  4. <book genre="fantasy" ISBN="2-3631-4">
  5. <title>Oberon's Legacy</title>
  6. <author>Corets, Eva</author>
  7. <price>5.95</price>
  8. </book>
  9. </bookstore>
  10. 显示所有数据。
  11. XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
  12. XmlNodeList xnl=xn.ChildNodes;
  13. foreach(XmlNode xnf in xnl)
  14. {
  15. XmlElement xe=(XmlElement)xnf;
  16. Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
  17. Console.WriteLine(xe.GetAttribute("ISBN"));
  18. XmlNodeList xnf1=xe.ChildNodes;
  19. foreach(XmlNode xn2 in xnf1)
  20. {
  21. Console.WriteLine(xn2.InnerText);//显示子节点点文本
  22. }
  23. }

转载于:https://www.cnblogs.com/BlessingIsMy/p/5506451.html

发表评论

表情:
评论列表 (有 0 条评论,312人围观)

还没有评论,来说两句吧...

相关阅读