I am creating a project template wizard which does replacement on an xml file. After the replacement, I want to format the whole xml document. Smart format is done automatically for class file (for example .cs files) but not .xml files in the project. I've tried different routes suggested in the forum:
- EditPoint.SmartFormat() doesn't work for XML file.
- DTE.ExecuteCommand("Edit.FormatDocument") fails when the document window isn't opened. (In IWizard implementation, it seems that even at RunFinished(), the project item window hasn't been completely activated so when I use this method, it throws an error)
Any workaround to do it for XML file, especially when the file hasn't been opened in Visual Studio yet?
For Each pi As ProjectItem In DTE.Solution.Projects.Item(1).ProjectItems
If (pi.Name.EndsWith(".xml")) Then
If Not pi.IsOpen(Constants.vsViewKindCode) Then
pi.Open(Constants.vsViewKindCode).Activate()
End If
Dim td As TextDocument = pi.Document.Object("TextDocument")
td.StartPoint.CreateEditPoint().SmartFormat(td.EndPoint)
'td.Selection.SelectAll()
'td.Selection.SmartFormat()
DTE.ExecuteCommand("Edit.FormatDocument")
End If
Next