T4 Template containing XML results in parse errors

Question!

I have created T4 Templates for the config files of my web and windows projects. I can successfully generate master web.config, and all configs for other environments, i.e. web.ci.config, etc..However, I could not get rid of the errors on my master tt files, such as :

  • Character '#', hexadecimal value 0x23 is illegal in an XML name.
  • Character '<', hexadecimal value 0x3c is illegal in XML attribute values.
  • Unexpected XML declaration. The XML declaration must be the first node in the document and no white space characters are allowed to appear before it.

I should be missing a xml schema or a reference, but what?


My file looks like:

<#@ template debug="true" hostSpecific="true" #>
<#@ output extension= ".config" #>  
<?xml version="1.0" encoding="utf-8"?>
<configuration>
...  
    <add key="FileUploadFolder" value="<#= this.FileUpload #>" /> 
...
</configuration>
<#+ 
   string FileUpload="\\\\server\\folder";
#>

And here is screenshot



Answers

What's happening here is that the Xml language service in VS is mistakenly identifying your T4 file as an Xml file, so spewing lots of false positive error messages.

I'm looking into whether there's a workaround to force it to ignore T4 files. If you could log a bug on Microsoft Connect, that would be helpful.

By : GarethJ


This video can help you solving your question :)
By: admin