Share via


Formatear una cadena XML en VB.NET

Buenas tardes,

Lo ultimo que se me presento fue leer un campo tipo XML de una base de datos SQL y mostrarlo en un formulario, todo estuvo muy bien pero al leer el campo XML y mostrarlo en un campo tipo TextBox se mostraba de manera desordenada y no mostraba ese aspecto ordenado jerarquicamente por niveles.

Deseo compatirles este código que les puede ayuda a formatear su cadena XML y mostrarlo de manera más elegante.

Private Sub  frmVisor_Load(sender As System.Object, e As  System.EventArgs) Handles MyBase.Load
        Dim docXML As New  XmlDocument()
 
        Try
            Cursor = Cursors.WaitCursor
            If txtDetalle.Text.Trim <> "" Then
                docXML.LoadXml(txtDetalle.Text)
                Using string_writer As  New System.IO.StringWriter()
                    Using xml_textwriter As  New XmlTextWriter(string_writer)
                        xml_textwriter.Formatting = Formatting.Indented
                        docXML.WriteTo(xml_textwriter)
                        xml_textwriter.Flush()
                        txtDetalle.Text = string_writer.ToString()
 
                    End Using
                End Using
 
            End If
        Catch ex As Exception
            Throw
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub

Si tienes alguna pregunta, no dudes en hacerla.

El Blog de Herbert Mendoza

Saludos
Herbert Mendoza