Visual Studio 宏:如何格式化 xml 文件?
我从 Visual Studio Macro to Format all Files in a Solution 派生了一个小宏脚本,但不幸的是它不适用于 xml、xaml 、配置等。所有基于 xml 的 ProjectItem
在主视图中打开时通常会抛出异常(命令不可用)vsViewKindPrimary
:
Dim projectItem As ProjectItem ' actually this is a parameter of a sub'
Dim window As Window = projectItem.Open(Constants.vsViewKindPrimary)
window.Activate()
projectItem.Document.DTE.ExecuteCommand("Edit.FormatDocument")
window.Close(vsSaveChanges.vsSaveChangesYes) ' actually this is part of a finally block'
结果:
System.Runtime.InteropServices.COMException (0x80004005): Command "Edit.FormatDocument" is not available.
at EnvDTE.DTEClass.ExecuteCommand(String CommandName, String CommandArgs)
打开时它们作为带有 vsViewKindTextView
的文本,尽管可以执行 Edit.FormatDocument
,但它们保持原样。
是否还有其他必须用于 xml 文件的命令?代码有问题吗?
I derived a small macro script from Visual Studio Macro to Format all Files in a Solution but unfortunately it doesn't work with xml, xaml, config etc. All ProjectItem
that are xml-based normally throw an exception (command not available) when they were opened in their primary view vsViewKindPrimary
:
Dim projectItem As ProjectItem ' actually this is a parameter of a sub'
Dim window As Window = projectItem.Open(Constants.vsViewKindPrimary)
window.Activate()
projectItem.Document.DTE.ExecuteCommand("Edit.FormatDocument")
window.Close(vsSaveChanges.vsSaveChangesYes) ' actually this is part of a finally block'
Results in:
System.Runtime.InteropServices.COMException (0x80004005): Command "Edit.FormatDocument" is not available.
at EnvDTE.DTEClass.ExecuteCommand(String CommandName, String CommandArgs)
When opening them as text with vsViewKindTextView
they stay as they are, although the Edit.FormatDocument
could be executed.
Is there another command that must be used for xml files? Is there something wrong with the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到了同样的问题,但重现略有不同。此代码适用于 .cpp 文件,但不适用于 .xml 文件:
如果我用以下内容替换最后一行,文件将保持不变:
我已经尝试了一段时间,但找不到解决方法。
I'm seeing the same issue, but have a slightly different repro. This code works for .cpp files, but not .xml files:
If I replace the last line with the following, it leaves the file unchanged:
I've tried for a while but can't find a work-around.