T4 Template containing XML results in parse errors

北慕城南 提交于 2019-12-05 14:01:32

Same problem here. I worked around it by replacing in the .tt file the line

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

with

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

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. Your whole template now looks like this:

<#@ 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";
#>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!