How do I display formatted XML in a SSRS Report?

大城市里の小女人 提交于 2019-12-07 13:28:43

问题


I have a field in my dataset that contains an unformatted XML string, for example:

<root><element><subelement>value</subelement></element></root>

How do I "prettify" it and display it within a Tablix control? Like this:

<root>
    <element>
        <subelement>value</subelement>
    </element>
</root>

回答1:


This can be achieved by using embedded code in the report and using System.Xml.XmlTextWriter with XmlTextWriterSettings.Indent = true

Open the Report Properties dialog and paste the following function in the Code tab:

Public Function FormatXml(input As String) As String
  Dim doc As New System.Xml.XmlDocument()
  doc.LoadXml(input)
  Dim sb As New System.Text.StringBuilder()
  Dim settings As New System.Xml.XmlWriterSettings()
  settings.Indent = True
  settings.IndentChars = "    "      ' This includes 4 non-breaking spaces: ALT+0160
  settings.NewLineChars = System.Environment.NewLine
  settings.NewLineHandling = System.Xml.NewLineHandling.Replace
  settings.OmitXmlDeclaration = True
  Using writer As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(sb, settings)
    doc.Save(writer)
  End Using
  Return sb.ToString()
End Function

You will also need to add a reference to System.Xml, since it is not included by default. Select the 'References' tab in Report Properties, then add System.Xml from the .NET assemblies list:

System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Then, within the expression of your textbox/table, you can use the following expression:

=Code.FormatXml(Fields!YourXmlField.Value)

When you try and deploy the report, you may receive an error like:

The definition of the report '/your.report' is invalid. Line 0:, Column: 0

This error message is not very useful, but it likely means that your embedded code is incorrect in some way. The most common cause is that you're referencing a class that it can't find. For example, XmlWriterSettings instead of System.Xml.XmlWriterSettings.



来源:https://stackoverflow.com/questions/19674094/how-do-i-display-formatted-xml-in-a-ssrs-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!