Introduction

In this article we learn what a Text Template Transformation Toolkit (T4) template is using XML. If you are unfamiliar with the T4 template then please read my article What a T4 Template is in MVC.

Let's create an empty MVC4 project.

  1. Go to File → Project.

  2. Select ASP.NET MVC 4 Web Application and name it T4TemplateUsingXml.

    mvc web application

  3. Click Ok.

  4. Select an Empty Project.

ASP NET MVC Project

After creating the project let's add a folder called Xml in the root directory.

ADD new Folder

Right-click on the Xml folder and add a XML file and name it Employee.xml.

ADD XML file

After creating Employee.xml paste in the following code.

  1. <Employee>
  2. <EmployeeDetail>
  3. <Id>E001</Id>
  4. <Name>Pramod</Name>
  5. <Email>[email protected]</Email>
  6. </EmployeeDetail>
  7. <EmployeeDetail>
  8. <Id>E002</Id>
  9. <Name>Ravi Kant</Name>
  10. <Email>[email protected]</Email>
  11. </EmployeeDetail>
  12. <EmployeeDetail>
  13. <Id>E003</Id>
  14. <Name>Rahul</Name>
  15. <Email>[email protected]</Email>
  16. </EmployeeDetail>
  17. <EmployeeDetail>
  18. <Id>E004</Id>
  19. <Name>Deepak</Name>
  20. <Email>[email protected]</Email>
  21. </EmployeeDetail>
  22. </Employee>

To create a model right-click on the Models folder and add a class and name it Employee and paste in the following code:

  1. using System;
  2. using System.Xml.Serialization;
  3. namespace T4TemplateUsingXml.Models
  4. {
  5. [Serializable]
  6. [XmlRoot("Employee"), XmlType("Employee")]
  7. public class Employee
  8. {
  9. public string Id { get; set; }
  10. public string Name { get; set; }
  11. public string Email { get; set; }
  12. }
  13. }

Now we create a controller, right-click on the Controller folder and add a controller named HomeController.

Home Controller

And then right-click on the index code inline and choose Add View using a scaffold empty template.

Add View

Add View1

Let's run the project and see the output.

Index

Now I make a copy of List.tt and name it PramodList.tt and make some changes. After that add another view called IndexXml using a scaffold PramodList template.

//Location of List.tt is C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView\CSHTML

Add View List

And write some code in HomeController as in the following:

  1. public ActionResult IndexXml()
  2. {
  3. string Data = Server.MapPath("~/Xml/Employee.xml");
  4. DataSet ds = new DataSet();
  5. ds.ReadXml(Data);
  6. var employee = new List<Employee>();
  7. employee = (from item in ds.Tables[0].AsEnumerable()
  8. select new Employee
  9. {
  10. Id = item[0].ToString(),
  11. Name = item[1].ToString(),
  12. Email = item[2].ToString(),
  13. }
  14. ).ToList();
  15. return View(employee);
  16. }

And run the application.
Index XML
Note: I am attaching a PramodList.tt along with the application. Please paste the PramodList.tt file into the following folder:

C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView\CSHTML

In the application I am just attaching Controller, Models, Views and XML folders.

I hope you enjoy this article.

Happy coding.