4

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

4

2 回答 2

6

这里同样的问题。我通过在.tt文件中替换该行来解决它

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

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

一旦 VS 对模板文件的 XML 格式感到困惑,它似乎会持续存在这种困惑——即使在像上面那样编辑并重新启动之后也是如此。解决这个问题的唯一方法似乎是.tt从您的项目中删除现有文件并从头开始重新创建它。

通过此更改,该.tt文件不再具有<?xml?>标记,因此 VS 不再将其视为 XML 文件。它忽略文字字符串中的所有内容。您的整个模板现在看起来像这样:

<#@ template debug="true" hostSpecific="true" #>
<#@ output extension= ".config" #>  
<# WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); #>
<configuration>
...  
    <add key="FileUploadFolder" value="<#= this.FileUpload #>" /> 
...
</configuration>
<#+ 
    string FileUpload="\\\\server\\folder";
#>
于 2012-07-03T13:52:16.487 回答
0

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.

于 2012-07-02T19:29:26.513 回答