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

  1. namespace MvcXML.Models
  2. {
  3. public class ProjectModels {
  4. public int Id {
  5. get;
  6. set;
  7. }
  8. [Required]
  9. public string ProjectName {
  10. get;
  11. set;
  12. }
  13. [Required]
  14. public string Location {
  15. get;
  16. set;
  17. }
  18. public bool IsEdit {
  19. get;
  20. set;
  21. }
  22. }
  23. }

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Projects>
  3. <Project>
  4. <Id>1</Id>
  5. <ProjectName>The Primus</ProjectName>
  6. <Location>Gurgaon</Location>
  7. </Project>
  8. <Project>
  9. <Id>2</Id>
  10. <ProjectName>DLF</ProjectName>
  11. <Location>Gudgoan</Location>
  12. </Project>
  13. <Project>
  14. <Id>4</Id>
  15. <ProjectName>Dev Builders</ProjectName>
  16. <Location>Grater Noida</Location>
  17. </Project>
  18. </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

  1. public class AdminController: Controller {
  2. //
  3. // GET: /Admin/
  4. public ActionResult Index() {
  5. List < ProjectModels > lstProject = new List < ProjectModels > ();
  6. DataSet ds = new DataSet();
  7. ds.ReadXml(Server.MapPath("~/XML/ProjectList.xml"));
  8. DataView dvPrograms;
  9. dvPrograms = ds.Tables[0].DefaultView;
  10. dvPrograms.Sort = "Id";
  11. foreach(DataRowView dr in dvPrograms) {
  12. ProjectModels model = new ProjectModels();
  13. model.Id = Convert.ToInt32(dr[0]);
  14. model.ProjectName = Convert.ToString(dr[1]);
  15. model.Location = Convert.ToString(dr[2]);
  16. lstProject.Add(model);
  17. }
  18. if (lstProject.Count > 0) {
  19. return View(lstProject);
  20. }
  21. return View();
  22. return View();
  23. }
  24. ProjectModels model = new ProjectModels();
  25. public ActionResult AddEditProject(int ? id) {
  26. int Id = Convert.ToInt32(id);
  27. if (Id > 0) {
  28. GetDetailsById(Id);
  29. model.IsEdit = true;
  30. return View(model);
  31. } else {
  32. model.IsEdit = false;
  33. return View(model);
  34. }
  35. }
  36. [HttpPost]
  37. public ActionResult AddEditProject(ProjectModels mdl) {
  38. if (mdl.Id > 0) {
  39. XDocument xmlDoc = XDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
  40. var items = (from item in xmlDoc.Descendants("Project") select item).ToList();
  41. XElement selected = items.Where(p => p.Element("Id").Value == mdl.Id.ToString()).FirstOrDefault();
  42. selected.Remove();
  43. xmlDoc.Save(Server.MapPath("~/XML/ProjectList.xml"));
  44. xmlDoc.Element("Projects").Add(new XElement("Project", new XElement("Id", mdl.Id), new XElement("ProjectName", mdl.ProjectName), new XElement("Location", mdl.Location)));
  45. xmlDoc.Save(Server.MapPath("~/XML/ProjectList.xml"));
  46. return RedirectToAction("Index", "Admin");
  47. } else {
  48. XmlDocument oXmlDocument = new XmlDocument();
  49. oXmlDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
  50. XmlNodeList nodelist = oXmlDocument.GetElementsByTagName("Project");
  51. var x = oXmlDocument.GetElementsByTagName("Id");
  52. int Max = 0;
  53. foreach(XmlElement item in x) {
  54. int EId = Convert.ToInt32(item.InnerText.ToString());
  55. if (EId > Max) {
  56. Max = EId;
  57. }
  58. }
  59. Max = Max + 1;
  60. XDocument xmlDoc = XDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
  61. xmlDoc.Element("Projects").Add(new XElement("Project", new XElement("Id", Max), new XElement("ProjectName", mdl.ProjectName), new XElement("Location", mdl.Location)));
  62. xmlDoc.Save(Server.MapPath("~/XML/ProjectList.xml"));
  63. return RedirectToAction("Index", "Admin");
  64. }
  65. }
  66. public ActionResult Delete(int Id) {
  67. if (Id > 0) {
  68. XDocument xmlDoc = XDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
  69. var items = (from item in xmlDoc.Descendants("Project") select item).ToList();
  70. XElement selected = items.Where(p => p.Element("Id").Value == Id.ToString()).FirstOrDefault();
  71. selected.Remove();
  72. xmlDoc.Save(Server.MapPath("~/XML/ProjectList.xml"));
  73. }
  74. return RedirectToAction("Index", "Admin");
  75. }
  76. public void GetDetailsById(int Id) {
  77. XDocument oXmlDocument = XDocument.Load(Server.MapPath("~/XML/ProjectList.xml"));
  78. var items = (from item in oXmlDocument.Descendants("Project") where Convert.ToInt32(item.Element("Id").Value) == Id select new projectItems {
  79. Id = Convert.ToInt32(item.Element("Id").Value),
  80. ProjectName = item.Element("ProjectName").Value,
  81. Location = item.Element("Location").Value,
  82. }).SingleOrDefault();
  83. if (items != null) {
  84. model.Id = items.Id;
  85. model.ProjectName = items.ProjectName;
  86. model.Location = items.Location;
  87. }
  88. }
  89. public class projectItems {
  90. public int Id {
  91. get;
  92. set;
  93. }
  94. public string ProjectName {
  95. get;
  96. set;
  97. }
  98. public string Location {
  99. get;
  100. set;
  101. }
  102. public projectItems() {}
  103. }
  104. }

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.

  1. @model IEnumerable
  2. <MvcXML.Models.ProjectModels>
  3. @{
  4. ViewBag.Title = "Index";
  5. }
  6. <style type="text/css">
  7. .topDiv
  8. {
  9. width: 50%;
  10. margin: 10px auto;
  11. background-color: #f2f2f2;
  12. text-align: left;
  13. padding: 2%;
  14. }
  15. </style>
  16. <div class="topDiv">
  17. <fieldset style="margin: 2% auto; width: 90%; background-color: #FFEBCD; text-align: center">
  18. <h4>
  19. @Html.ActionLink("Add New project", "AddEditProject")
  20. </h4>
  21. </fieldset>
  22. <fieldset>
  23. <table style="margin: 2% auto; padding: 5px; width: 90%">
  24. <tr style="background-color: #FFFACD">
  25. <th>
  26. ProjectName
  27. </th>
  28. <th>
  29. Location
  30. </th>
  31. <th>
  32. Manage
  33. </th>
  34. </tr>
  35. @{
  36. foreach (var item in Model)
  37. {
  38. <tr style="background-color: #FFFFF0">
  39. <td>
  40. @item.ProjectName
  41. </td>
  42. <td>
  43. @item.Location
  44. </td>
  45. <td>
  46. @Html.ActionLink("Edit", "AddEditProject", new { id = @item.Id }) / @Html.ActionLink("Delete", "Delete", new { id = @item.Id })
  47. @* /@Html.ActionLink("Details", "ProjectDetails", new { basePath = @item.basePath })*@
  48. </td>
  49. </tr>
  50. }
  51. }
  52. </table>
  53. </fieldset>
  54. </div>

Now, again go to AdminController and create new view for AddEditProject action with a right click and write the following code:

AddEditProject.cshtml

  1. @model MvcXML.Models.ProjectModels
  2. @{
  3. ViewBag.Title = "AddEditProject";
  4. }
  5. <style type="text/css">
  6. .topDiv
  7. {
  8. width: 50%;
  9. margin: 10px auto;
  10. background-color: #f2f2f2;
  11. text-align: center;
  12. padding: 2%;
  13. }
  14. .innerDiv
  15. {
  16. margin: 0 auto;
  17. padding: 1%;
  18. color: Gray;
  19. }
  20. </style>
  21. @using (Html.BeginForm())
  22. {
  23. <div class="topDiv">
  24. <table>
  25. <tr>
  26. <h2>Add/Edit User</h2>
  27. </tr>
  28. <tr>
  29. <td>
  30. @Html.LabelFor(m => m.ProjectName) :
  31. </td>
  32. <td>
  33. <div class="innerDiv">
  34. @Html.TextBoxFor(m => m.ProjectName)
  35. @Html.ValidationMessageFor(m => m.ProjectName)
  36. </div>
  37. </td>
  38. </tr>
  39. <tr>
  40. <td>
  41. @Html.LabelFor(m => m.Location) :
  42. </td>
  43. <td>
  44. <div class="innerDiv">
  45. @Html.TextBoxFor(m => m.Location)
  46. @Html.ValidationMessageFor(m => m.ProjectName)
  47. </div>
  48. </td>
  49. </tr>
  50. <tr>
  51. <td colspan="2">
  52. <div class="innerDiv">
  53. <input id="btnAdd" type="submit" value="@if (Model.IsEdit)
  54. {
  55. <text>Update</text>}
  56. else
  57. {
  58. <text>Add</text>}" style=" width:75px" />
  59. </div>
  60. </td>
  61. </tr>
  62. </table>
  63. </div>
  64. }

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.