Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2012 T4 templates generating XML gives error

I'm using visual studio 2012 to generate XML from a T4 template. The top of the template looks like

<#@ template language="VBv4" hostspecific="True" debug="True" #>
<#@ output extension="wmef" #>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>

The T4 template correctly generates the XML. This is no problem. However visual studio is getting confused and it additionally tries to process the template itself as XML and reports build errors saying the XML is ill formed which of course it is because it is a template for XML and not the actual XML.

When I first edit the .tt file everything is ok. However once the XML file is generated and I load it in the editor to look at it and then go back to the .tt file suddenly the .tt file is syntax highlighted to look like XML and then the build errors appear.

Perhapps this is just a bug for m$oft

Any ideas on what might be happening here?

like image 419
bradgonesurfing Avatar asked Sep 04 '12 08:09

bradgonesurfing


1 Answers

This is a bug in VS. You can work around it by replacing in the .tt file the line

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

with

<# WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>"); #>

Once VS is confused about the XML format of the template file, it seems to persist in that confusion -- even after editing like above and a restart. The only way around that seems to be to delete the existing .tt file from your project and re-create it from scratch.

With this change, the .tt file does not have a <?xml?> tag anymore so VS does not consider it an XML file. It ignores everything inside the literal string.

This seems to be the same issue as T4 Template containing XML results in parse errors, but the workaround was never confirmed or accepted by the OP, which is why I repost it here.

like image 113
Reinier Torenbeek Avatar answered Oct 25 '22 07:10

Reinier Torenbeek