我已经为我的 web 和 windows 项目的配置文件创建了 T4 模板。我可以成功生成主 web.config 和其他环境的所有配置,即 web.ci.config 等。但是,我无法消除主 tt 文件中的错误,例如:
- 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.
我应该缺少 xml 架构或引用,但是什么?
我的文件看起来像:
<#@ 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";
#>
这里是 screenshot
最佳答案
同样的问题。我通过替换 .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";
#>
关于.net - 包含 XML 的 T4 模板导致解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11293963/