DCSIMG
Reading a Xml File in T4 Templates - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Reading a Xml File in T4 Templates

Reading a Xml File in T4 Templates

After I wrote the Generating Code From a File Using T4 Templates
post about the
use of T4 templates
in EF4
, I played with
them for a while (not
in EF but generally with
T4 templates).
In an old project that
I’ve created almost 3 years ago
which automated the use of lookup tables I had an enum.
That enum was meant to be the connection between an Xml
node names and was heavily used in the application.
For each Xml node I needed to add an entry in the enum.
So I thought what the hell lets see if I could have make it using
a T4 template. The result is in the post.

Reading a Xml File in T4 Templates

The first thing to do is to add a new file with the extension of
.tt into the project. After I had that T4 template file I started to
think what is the way to read an Xml file from the T4. Since the
Xml file was a part of the project the use of Path class to get the
absolute location was one way to get the file.
Pay attention that if you will open the solution from the IDE and
not from its location (double click on the solution from its current
location) the Path.GetFullPath won’t work.
After I had the file it was as simple as to read a Xml using
XmlDocument and XPath query.
The following example is the T4 I have created:

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
using System;
 
namespace Examples
{
    public class ExampleClass
    {
        #region Enums
 
        public enum eExampleEnum
        {
<#
    foreach (string name in this.GetNames())   
    {
#>
    <#= name #>,
<# 
    }
        #>
        }
        
        #endregion
    }
}
<#+
    public List<string> GetNames()
    {
        List<string> result = new List<string>(); 
        XmlDocument doc = new XmlDocument();
        string absolutePath = Path.GetFullPath("File.xml");                
        doc.Load(absolutePath);
        foreach (XmlNode node in doc.SelectNodes("/Root/Element"))
        {
            result.Add(node.Attributes["Name"].InnerText);
        }
        return result;
    }
#>

Summary

The use of T4 template can help us to generate code using
a code generation tool. In the example I showed how you can use
a T4 template in order to generate an enum from a given Xml file.

DotNetKicks Image

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# December 12, 2009 7:09 PM

Tyrone said:

I think using this method would work in all cases

<#@ template language="C#" hostspecific="True" #>

...

string absolutePath = Host.ResolvePath("File.xml");

...

This will look in the current project's folder for the file.

# December 14, 2009 12:05 AM

Reflective Perspective - Chris Alcock » The Morning Brew #497 said:

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #497

# December 14, 2009 11:40 AM

links for 2009-12-14 « dstelow notes… said:

Pingback from  links for 2009-12-14 &laquo;  dstelow notes&#8230;

# December 15, 2009 1:03 AM

T4: Call function « Nathan said:

Pingback from  T4: Call function &laquo; Nathan

# December 14, 2011 12:12 AM