Visual Studio 宏:如何格式化 xml 文件?

发布于 2024-11-08 04:57:15 字数 942 浏览 5 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-11-15 04:57:15

我看到了同样的问题,但重现略有不同。此代码适用于 .cpp 文件,但不适用于 .xml 文件:

EnvDTE.Solution soln = System.Activator.CreateInstance(
    Type.GetTypeFromProgID("VisualStudio.Solution.11.0")) as EnvDTE.Solution;

soln.DTE.ItemOperations.OpenFile(file);
soln.DTE.ExecuteCommand("Edit.FormatDocument");

如果我用以下内容替换最后一行,文件将保持不变:

TextSelection selection = soln.DTE.ActiveDocument.Selection as TextSelection;
selection.SelectAll();
selection.SmartFormat();

我已经尝试了一段时间,但找不到解决方法。

I'm seeing the same issue, but have a slightly different repro. This code works for .cpp files, but not .xml files:

EnvDTE.Solution soln = System.Activator.CreateInstance(
    Type.GetTypeFromProgID("VisualStudio.Solution.11.0")) as EnvDTE.Solution;

soln.DTE.ItemOperations.OpenFile(file);
soln.DTE.ExecuteCommand("Edit.FormatDocument");

If I replace the last line with the following, it leaves the file unchanged:

TextSelection selection = soln.DTE.ActiveDocument.Selection as TextSelection;
selection.SelectAll();
selection.SmartFormat();

I've tried for a while but can't find a work-around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文