Visual Studio 2012 T4 模板生成 XML 出错 [英] Visual Studio 2012 T4 templates generating XML gives error

查看:35
本文介绍了Visual Studio 2012 T4 模板生成 XML 出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio 2012 从 T4 模板生成 XML.模板顶部看起来像

I'm using visual studio 2012 to generate XML from a T4 template. The top of the template looks like

<#@ template language="VBv4" hostspecific="True" debug="True" #>
<#@ output extension="wmef" #>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>

T4 模板正确生成 XML.这没有问题.然而,visual studio 越来越困惑,它还尝试将模板本身作为 XML 处理,并报告构建错误,说 XML 格式错误,这当然是因为它是 XML 的模板,而不是实际的 XML.

The T4 template correctly generates the XML. This is no problem. However visual studio is getting confused and it additionally tries to process the template itself as XML and reports build errors saying the XML is ill formed which of course it is because it is a template for XML and not the actual XML.

当我第一次编辑 .tt 文件时,一切正常.然而,一旦生成了 XML 文件并且我将它加载到编辑器中查看它,然后突然返回 .tt 文件,.tt 文件的语法高亮显示为看起来像 XML,然后出现构建错误.

When I first edit the .tt file everything is ok. However once the XML file is generated and I load it in the editor to look at it and then go back to the .tt file suddenly the .tt file is syntax highlighted to look like XML and then the build errors appear.

也许这只是 m$oft 的一个错误

Perhapps this is just a bug for m$oft

对这里可能发生的事情有什么想法吗?

Any ideas on what might be happening here?

推荐答案

这是 VS 中的一个错误.您可以通过替换 .tt 文件中的行

This is a bug in VS. You can work around it by replacing in the .tt file the line

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

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

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

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.

通过此更改,.tt 文件不再具有 <?xml?> 标记,因此 VS 不会将其视为 XML 文件.它忽略文字字符串中的所有内容.

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.

这似乎与 包含 XML 导致解析错误的 T4 模板 相同,但解决方法从未得到证实或被 OP 接受,这就是我在此处重新发布的原因.

This seems to be the same issue as T4 Template containing XML results in parse errors, but the workaround was never confirmed or accepted by the OP, which is why I repost it here.

这篇关于Visual Studio 2012 T4 模板生成 XML 出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆