开发者

Visual Studio Macro: How to format xml files?

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.Runtim开发者_开发知识库e.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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜