简体   繁体   English

SQL Server XML查询格式

[英]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 --- 当我使用FOR XML语句查询SQL Server时,我得到了预期的结果,格式如下......没有适当的缩进---

<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>

点击这里

When you run your query in GRID mode, the XML column is hyperlinked. 在GRID模式下运行查询时,XML列是超链接的。
The hint appears when you hover over it. 当您将鼠标悬停在其上时,会显示提示。
Click it as shown to reveal a formatted XML tab. 单击它,如图所示,以显示格式化的XML选项卡。

SQL Server doesn't have any way to "tidy" XML results. SQL Server没有任何方法可以“整理”XML结果。 You'll need to use a third-party tool to do that. 您需要使用第三方工具来执行此操作。 NotePad++ has some XML formatting tools that do well, as long as you're not working with ridiculously large XML (aka 100MB) files. NotePad ++有一些XML格式化工具可以很好地完成,只要你不使用可笑的大型XML(又名100MB)文件。

If you just want a really basic formatting, you can do a string replacement of >< with >\\n< or something like that with your front-end. 如果你只想要一个非常基本的格式化,你可以用你的前端替换>< with >\\n<或类似的字符串。 Your performance will be EXTREMELY poor trying to do any sort of formatting of the XML in SQL Server. 尝试在SQL Server中进行任何类型的XML格式化时,您的性能将非常差。 DO NOT attempt to format your XML on SQL Server. 不要尝试在SQL Server上格式化XML。 Ever. 永远。

If you're running a .Net front-end, you can try plugging in something like TidyForNet to pretty up the XML, or you can run it through an XSLT transform (not preferred, IMHO). 如果你正在运行.Net前端,你可以尝试插入像TidyForNet这样的东西来提升XML,或者你可以通过XSLT转换(不是首选,恕我直言)来运行它。

NOTE: If you just need to run a query once and view the resultant XML in "tidy" format, check RichardTheKiwi's answer . 注意:如果您只需要运行一次查询并以“整洁”格式查看生成的XML,请检查RichardTheKiwi的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM