How C # MVC gets an XML file

Source: Internet
Author: User

The XML file format is as follows:
Documents are mainly used to record Chinese traditional festivals.

[HTML] View plaincopy
<?xml version= "1.0" encoding= "Utf-8"?>
<China>
<Festival>
<title> Qingming </title>
<date>4-5</date>
<contents> the days of Chinese people commemorating their ancestors </contents>
</Festival>
</China>

Code for XMltestController.cs in controllers in MVC
LINQ to XML single information display
var xDoc = xdocument.load (Server.MapPath ("/content/jieri.xml"));
var xmlData = from item in xdoc.descendants ("Festival") where item. Element ("title"). Value = = "Chongyang" 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 ();

The structure of this cycle must be used, otherwise there is no way to show it.
foreach (var item in XmlData)
{
Je.title = Item.title;
Je.date = item.date;
Je.contents = item.contents;
}
Return View (JE);
}

<summary>
LINQ to XML List functionality
</summary>
<returns></returns>
Public ActionResult List ()
{
var xdoc = xelement.load (Server.MapPath ("/content/jieri.xml"));

The new Jieris here defines a model class, which in turn will put the code on the back, which is three fields
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 Add functionality
</summary>
<returns></returns>
Public ActionResult Add ()
{
var xdoc = xelement.load (Server.MapPath ("/content/jieri.xml"));
Add to
XElement XE = new XElement ("Festival",
New XElement ("title", "Laba"),
New XElement ("date", "12-8"),
New XElement ("Contents", "the beginning of the Lunar December Eight (the lunar calendar December is called"), is the traditional Chinese Han Laba festival, this day in most areas of our country have the custom of eating laba porridge. "));
Xdoc. ADD (XE);
Xdoc. Save (Server.MapPath ("/content/jieri.xml"));
Return redirecttoaction ("List");
}

<summary>
LINQ to XML modification features
</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 Delete feature
</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");
}

The change here is very simple to change the name, did not do any processing, and did not make judgments.

One thing to illustrate here is that sometimes
var xDoc = xdocument.load (Server.MapPath ("/content/jieri.xml"));
This is to initialize an XML, in fact, this initialization is also feasible, but one thing to note is that for the list page and Detail page This is not a problem, but for, add, modify, delete will appear problems

Front Code
<%@ 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 ">
<title>List</title>
<body>
<table>
<tr>
<th></th>
<th>
Title
</th>
<th>
Date
</th>
<th>
Contents
</th>
</tr>

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

<tr>
<td>
<%= Html.ActionLink ("Modify", "edit", new {id=item.date})%> |
<%= Html.ActionLink ("Remove", "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>

Model class 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;}
}
}

Example 2

It is known that an XML file (Bookstore.xml) is as follows:
<?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>

Show all data.
XmlNode Xn=xmldoc.selectsinglenode ("bookstore");
XmlNodeList Xnl=xn. ChildNodes;

foreach (XmlNode xnf in XNL)
{
XmlElement xe= (XmlElement) xnf;
Console.WriteLine (XE. GetAttribute ("genre"));//Display property values
Console.WriteLine (XE. GetAttribute ("ISBN"));

XmlNodeList Xnf1=xe. ChildNodes;
foreach (XmlNode xn2 in Xnf1)
{
Console.WriteLine (xn2. InnerText);//Display child node point text
}
}

How C # MVC gets an XML file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.