110
votes

Is there an easy way to reformat an XML file while viewing it Visual Studio 2010. For example, if you open a generated app.config file, it might look like:

<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

This is difficult to read, is there a way to instruct Visual Studio 2010 to format this, so that it looks more like:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

Thanks for any help.

Update: With credit to Julien Hoarau for one of the answers. The answers are:

Edit -> Advanced -> Format Document (Ctrl+K, Ctrl+D)
Edit -> Advanced -> Format Selection (Ctrl+K, Ctrl+F)
3
Just want to mention a handy option: if you want to enable/disable auto-formatting on paste from clipboard Tools → Options → Text Editor → XML → Formatting → "On paste from clipboard" Corio

3 Answers

175
votes

Yes you can :

Edit > Advanced > Format document (Ctrl+K Ctrl+D)
6
votes

If you want to leave your right hand free to do something else, you can use Ctrl+E, Ctrl+D.

Not sure how this is different from Ctrl+K, Ctrl+D.

1
votes

Not 100% sure about VS2010, but in VS2015 if you copy and paste a long string of XML into an .XML document, it will automatically reformat it.

("Format Document" is usually the way to go, just mentioning another option.)