sqlsql-serverxmlformattingxml-formatting

SQL Server XML Query formatting


When I query SQL Server with a FOR XML statement, I get the expected the result in the following format...that is without proper indentation ---

<ROOT><SUBROOT><A>1111</A></SUBROOT><SUBROOT><B>2222</B></SUBROOT><ROOT>  

How do I format this result or query it to get in the following format?

<ROOT>
 <SUBROOT>
  <A>1111</A>
 </SUBROOT>
 <SUBROOT>
  <B>2222</B>
 </SUBROOT>
<ROOT>

Solution

  • Click here

    When you run your query in GRID mode, the XML column is hyperlinked.
    The hint appears when you hover over it.
    Click it as shown to reveal a formatted XML tab.