XML CRUD (Create Read Update Delete) Operation Using MVC

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. @{  
  5.   
  6. ViewBag.Title = "Index";  
  7.   
  8. }  
  9.   
  10.   
  11.     <style type="text/css">  
  12.   
  13. .topDiv  
  14.   
  15. {  
  16.   
  17. width: 50%;  
  18.   
  19. margin: 10px auto;  
  20.   
  21. background-color: #f2f2f2;  
  22.   
  23. text-align: left;  
  24.   
  25. padding: 2%;  
  26.   
  27. }  
  28.   
  29. </style>  
  30.     <div class="topDiv">  
  31.         <fieldset style="margin: 2% auto; width: 90%; background-color: #FFEBCD; text-align: center">  
  32.             <h4>  
  33.   
  34. @Html.ActionLink("Add New project", "AddEditProject")  
  35.   
  36. </h4>  
  37.         </fieldset>  
  38.         <fieldset>  
  39.             <table style="margin: 2% auto; padding: 5px; width: 90%">  
  40.                 <tr style="background-color: #FFFACD">  
  41.                     <th>  
  42.   
  43. ProjectName  
  44.   
  45. </th>  
  46.                     <th>  
  47.   
  48. Location  
  49.   
  50. </th>  
  51.                     <th>  
  52.   
  53. Manage  
  54.   
  55. </th>  
  56.                 </tr>  
  57.   
  58. @{  
  59.   
  60. foreach (var item in Model)  
  61.   
  62. {  
  63.   
  64.   
  65.                 <tr style="background-color: #FFFFF0">  
  66.                     <td>  
  67.   
  68. @item.ProjectName  
  69.   
  70. </td>  
  71.                     <td>  
  72.   
  73. @item.Location  
  74.   
  75. </td>  
  76.                     <td>  
  77.   
  78. @Html.ActionLink("Edit", "AddEditProject", new { id = @item.Id }) / @Html.ActionLink("Delete", "Delete", new { id = @item.Id })  
  79.   
  80. @* /@Html.ActionLink("Details", "ProjectDetails", new { basePath = @item.basePath })*@  
  81.   
  82. </td>  
  83.                 </tr>  
  84.   
  85. }  
  86.   
  87. }  
  88.   
  89.   
  90.             </table>  
  91.         </fieldset>  
  92.     </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. @{  
  4.   
  5. ViewBag.Title = "AddEditProject";  
  6.   
  7. }  
  8.   
  9.   
  10. <style type="text/css">  
  11.   
  12. .topDiv  
  13.   
  14. {  
  15.   
  16. width: 50%;  
  17.   
  18. margin: 10px auto;  
  19.   
  20. background-color: #f2f2f2;  
  21.   
  22. text-align: center;  
  23.   
  24. padding: 2%;  
  25.   
  26. }  
  27.   
  28.   
  29. .innerDiv  
  30.   
  31. {  
  32.   
  33. margin: 0 auto;  
  34.   
  35. padding: 1%;  
  36.   
  37. color: Gray;  
  38.   
  39. }  
  40.   
  41.   
  42. </style>  
  43.   
  44. @using (Html.BeginForm())  
  45.   
  46. {  
  47.   
  48.   
  49.   
  50. <div class="topDiv">  
  51.     <table>  
  52.         <tr>  
  53.             <h2>Add/Edit User</h2>  
  54.         </tr>  
  55.         <tr>  
  56.             <td>  
  57.   
  58. @Html.LabelFor(m => m.ProjectName) :  
  59.   
  60. </td>  
  61.             <td>  
  62.                 <div class="innerDiv">  
  63.   
  64. @Html.TextBoxFor(m => m.ProjectName)  
  65.   
  66. @Html.ValidationMessageFor(m => m.ProjectName)  
  67.   
  68. </div>  
  69.             </td>  
  70.         </tr>  
  71.         <tr>  
  72.             <td>  
  73.   
  74. @Html.LabelFor(m => m.Location) :  
  75.   
  76. </td>  
  77.             <td>  
  78.                 <div class="innerDiv">  
  79.   
  80. @Html.TextBoxFor(m => m.Location)  
  81.   
  82. @Html.ValidationMessageFor(m => m.ProjectName)  
  83.   
  84. </div>  
  85.             </td>  
  86.         </tr>  
  87.         <tr>  
  88.             <td colspan="2">  
  89.                 <div class="innerDiv">  
  90.                     <input id="btnAdd" type="submit" value="@if (Model.IsEdit)  
  91.   
  92. {  
  93.                         <text>Update</text>}  
  94.   
  95. else  
  96.   
  97. {  
  98.                         <text>Add</text>}" style=" width:75px" />  
  99.                     </div>  
  100.                 </td>  
  101.             </tr>  
  102.         </table>  
  103.     </div>  
  104.   
  105. }  

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.


Similar Articles