T4 Template Using XML

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.    
  3.      <EmployeeDetail>  
  4.          <Id>E001</Id>  
  5.          <Name>Pramod</Name>  
  6.          <Email>[email protected]</Email>  
  7.      </EmployeeDetail>  
  8.    
  9.      <EmployeeDetail>  
  10.          <Id>E002</Id>  
  11.          <Name>Ravi Kant</Name>  
  12.          <Email>[email protected]</Email>  
  13.      </EmployeeDetail>  
  14.    
  15.      <EmployeeDetail>  
  16.          <Id>E003</Id>  
  17.          <Name>Rahul</Name>  
  18.          <Email>[email protected]</Email>  
  19.      </EmployeeDetail>  
  20.    
  21.      <EmployeeDetail>  
  22.          <Id>E004</Id>  
  23.          <Name>Deepak</Name>  
  24.          <Email>[email protected]</Email>  
  25.      </EmployeeDetail>  
  26.    
  27.  </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 { getset; }  
  10.          public string Name { getset; }  
  11.          public string Email { getset; }  
  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.  }  
  17.    

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.

Up Next
    Ebook Download
    View all
    Learn
    View all