Visual Studio 2012 T4 templates generating XML gives error

半腔热情 提交于 2019-12-22 05:06:59

问题


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

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.

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.

Perhapps this is just a bug for m$oft

Any ideas on what might be happening here?


回答1:


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.




回答2:


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.


来源:https://stackoverflow.com/questions/12259217/visual-studio-2012-t4-templates-generating-xml-gives-error

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