博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# mvc如何获取xml文件
阅读量:6885 次
发布时间:2019-06-27

本文共 4321 字,大约阅读时间需要 14 分钟。

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" ""> 
<html xmlns="" > 
<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)如下: 
Oberon's Legacy
Corets, Eva
5.95
显示所有数据。 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);//显示子节点点文本 } }

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

你可能感兴趣的文章
说好的不熬夜呢???!!!! -- 超市项目
查看>>
Apache遇到的问题:APR not found
查看>>
运行webpack-dev-srerver 端口占用错误及解决办法
查看>>
html-php深入理解
查看>>
第 11 章 日志管理 - 088 - Docker 如何支持多种日志方案?
查看>>
课后作业-----输入法评价
查看>>
使用qemu
查看>>
静态页之间传值
查看>>
01.Hibernate快速入门
查看>>
ThinkPHP3.2判断是否为手机端访问并跳转到另一个模块的方法
查看>>
人事管理系统——11个基础信息管理界面
查看>>
关于联想超极本出现蓝屏Default Boot Device Missing or Boot Failed的解决办法
查看>>
solr索引报错(java.lang.OutOfMemoryError:GC overhead limit exceeded)
查看>>
python基础2--小结篇
查看>>
Ajax传统操作
查看>>
webpack01
查看>>
NoSQL 简介
查看>>
功能测试思考点
查看>>
PHP代码审计
查看>>
杭电4786--Fibonacci Tree(生成树)
查看>>