Step 1. Creating an MVC Application
- Open Visual Studio
- File à New Project…
- Select ASP.NET MVC3/4 Web Application.
- Enter the name of Application as "CodeFirstDemo".
- Click OK.
Step 2. Creating the Model
Right click on the Models folder and create a model with the name of "ProjectModels" and create properties, as shown in the code, given below:
ProjectModels.cs
- namespace MvcXML.Models
- {
- public class ProjectModels {
- public int Id {
- get;
- set;
- }
- [Required]
- public string ProjectName {
- get;
- set;
- }
- [Required]
- public string Location {
- get;
- set;
- }
- public bool IsEdit {
- get;
- set;
- }
- }
- }
Step 3. Create a XML File
Create a folder with the name of XML, as shown in the following picture, and create an XML file with the name of "ProjectList.XML", using Add a New Item by right clicking on XML folder. Write the code as follows in XML file.
ProjectList.xml
- <?xml version="1.0" encoding="utf-8"?>
- <Projects>
- <Project>
- <Id>1</Id>
- <ProjectName>The Primus</ProjectName>
- <Location>Gurgaon</Location>
- </Project>
- <Project>
- <Id>2</Id>
- <ProjectName>DLF</ProjectName>
- <Location>Gudgoan</Location>
- </Project>
- <Project>
- <Id>4</Id>
- <ProjectName>Dev Builders</ProjectName>
- <Location>Grater Noida</Location>
- </Project>
- </Projects>
Step 4 : Create a Controller.
Right click on the Controller folder and create a controller with the name of "AdminController", as shown in the picture below. Write the code given below in Admincontroller within an Index Action.
AdminController.cs
- public class AdminController: Controller {
-
-
- public ActionResult Index() {
- List < ProjectModels > lstProject = new List < ProjectModels > ();
- DataSet ds = new DataSet();
- ds.ReadXml(Server.MapPath("~/XML/ProjectList.xml"));
- DataView dvPrograms;
- dvPrograms = ds.Tables[0].DefaultView;
- dvPrograms.Sort = "Id";
- foreach(DataRowView dr in dvPrograms) {
- ProjectModels model = new ProjectModels();
- model.Id = Convert.ToInt32(dr[0]);
- model.ProjectName = Convert.ToString(dr[1]);
- model.Location = Convert.ToString(dr[2]);
- lstProject.Add(model);
- }
- if (lstProject.Count > 0) {
- return View(lstProject);
- }
- return View();
- return View();
- }
- ProjectModels model = new ProjectModels();
- public ActionResult AddEditProject(int ? id) {
- int Id = Convert.ToInt32(id);
- if (Id > 0) {
- GetDetailsById(Id);
- model.IsEdit = true;
- return View(model);
- } else {
- model.IsEdit = false;
- return View(model);
- }
- }
- [HttpPost]
- public ActionResult AddEditProject(ProjectModels mdl) {
- if (mdl.Id > 0) {
- XDocument xmlDoc = XDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
- var items = (from item in xmlDoc.Descendants("Project") select item).ToList();
- XElement selected = items.Where(p => p.Element("Id").Value == mdl.Id.ToString()).FirstOrDefault();
- selected.Remove();
- xmlDoc.Save(Server.MapPath("~/XML/ProjectList.xml"));
- xmlDoc.Element("Projects").Add(new XElement("Project", new XElement("Id", mdl.Id), new XElement("ProjectName", mdl.ProjectName), new XElement("Location", mdl.Location)));
- xmlDoc.Save(Server.MapPath("~/XML/ProjectList.xml"));
- return RedirectToAction("Index", "Admin");
- } else {
- XmlDocument oXmlDocument = new XmlDocument();
- oXmlDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
- XmlNodeList nodelist = oXmlDocument.GetElementsByTagName("Project");
- var x = oXmlDocument.GetElementsByTagName("Id");
- int Max = 0;
- foreach(XmlElement item in x) {
- int EId = Convert.ToInt32(item.InnerText.ToString());
- if (EId > Max) {
- Max = EId;
- }
- }
- Max = Max + 1;
- XDocument xmlDoc = XDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
- xmlDoc.Element("Projects").Add(new XElement("Project", new XElement("Id", Max), new XElement("ProjectName", mdl.ProjectName), new XElement("Location", mdl.Location)));
- xmlDoc.Save(Server.MapPath("~/XML/ProjectList.xml"));
- return RedirectToAction("Index", "Admin");
- }
- }
- public ActionResult Delete(int Id) {
- if (Id > 0) {
- XDocument xmlDoc = XDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
- var items = (from item in xmlDoc.Descendants("Project") select item).ToList();
- XElement selected = items.Where(p => p.Element("Id").Value == Id.ToString()).FirstOrDefault();
- selected.Remove();
- xmlDoc.Save(Server.MapPath("~/XML/ProjectList.xml"));
- }
- return RedirectToAction("Index", "Admin");
- }
- public void GetDetailsById(int Id) {
- XDocument oXmlDocument = XDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
- var items = (from item in oXmlDocument.Descendants("Project") where Convert.ToInt32(item.Element("Id").Value) == Id select new projectItems {
- Id = Convert.ToInt32(item.Element("Id").Value),
- ProjectName = item.Element("ProjectName").Value,
- Location = item.Element("Location").Value,
- }).SingleOrDefault();
- if (items != null) {
- model.Id = items.Id;
- model.ProjectName = items.ProjectName;
- model.Location = items.Location;
- }
- }
- public class projectItems {
- public int Id {
- get;
- set;
- }
- public string ProjectName {
- get;
- set;
- }
- public string Location {
- get;
- set;
- }
- public projectItems() {}
- }
- }
Now, create a View for the action, using right click on the controller's action and write the code as follows for the Index and AddEditProject respectively.
Index.cshtml file.
- @model IEnumerable
- <MvcXML.Models.ProjectModels>
-
- @{
-
- ViewBag.Title = "Index";
-
- }
-
-
- <style type="text/css">
-
- .topDiv
-
- {
-
- width: 50%;
-
- margin: 10px auto;
-
- background-color: #f2f2f2;
-
- text-align: left;
-
- padding: 2%;
-
- }
-
- </style>
- <div class="topDiv">
- <fieldset style="margin: 2% auto; width: 90%; background-color: #FFEBCD; text-align: center">
- <h4>
-
- @Html.ActionLink("Add New project", "AddEditProject")
-
- </h4>
- </fieldset>
- <fieldset>
- <table style="margin: 2% auto; padding: 5px; width: 90%">
- <tr style="background-color: #FFFACD">
- <th>
-
- ProjectName
-
- </th>
- <th>
-
- Location
-
- </th>
- <th>
-
- Manage
-
- </th>
- </tr>
-
- @{
-
- foreach (var item in Model)
-
- {
-
-
- <tr style="background-color: #FFFFF0">
- <td>
-
- @item.ProjectName
-
- </td>
- <td>
-
- @item.Location
-
- </td>
- <td>
-
- @Html.ActionLink("Edit", "AddEditProject", new { id = @item.Id }) / @Html.ActionLink("Delete", "Delete", new { id = @item.Id })
-
- @* /@Html.ActionLink("Details", "ProjectDetails", new { basePath = @item.basePath })*@
-
- </td>
- </tr>
-
- }
-
- }
-
-
- </table>
- </fieldset>
- </div>
Now, again go to AdminController and create new view for AddEditProject action with a right click and write the following code:
AddEditProject.cshtml
Now, press F5 to run the Application,
I hope your browser will look as shown below:
After clicking Add New Project and Edit link, your view will be as shown below:
Summary
In this walkthrough, we looked at the development for XML CRUD operation, using MVC. We defined XML class and use the class for CRUD operations.