Visual Studio 2012 T4 templates generating XML gives error

Deadly 提交于 2019-12-05 05:52:11
Reinier Torenbeek

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"?>

with

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

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.

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.

As a follow up to Reinier's answer...

I found that deleting and recreating the file works, but it is not necessary to exclude the file from the project.

I found the fastest/easiest way was to:

  1. Close the file.
  2. Delete the T4 file only, using Windows Explorer. This way the .proj file is not affected.
  3. Bring them back again by getting latest from your source control.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!