.net - 包含 XML 的 T4 模板导致解析错误

标签 .net xml config t4 transformation

我已经为我的 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/

相关文章:

c# - HashSet<T> Xml 是可序列化的吗?

laravel - 无法更改 Laravel 中的默认文件系统磁盘,因为 filesystem.php 中的设置被忽略

c# - 如何使用 C# 正确卸载 AppDomain?

.net - 电子邮件的正则表达式不接受超过一个点或撇号

c# - ASP.NET Web API 中的 XML 命名空间

javascript - Webpack 输出是反向的

java - 没有 web.xml 的 Wicket 口;纯java配置

c# - CloudBlob.DownloadText 方法插入附加字符?

.net - Console.WriteLine() 与 Debug.WriteLine() 有什么区别?

java - 在jsp中从mysql获取xml格式的数据