Showing correctly formatted XML in textbox

davearia

Centurion
Joined
Jan 4, 2005
Messages
184
Hi,

I am putting a xml document into a textbox. The only problem is that all the indentation etc in the original document is lost when it is put in the textbox. So your left with an ugly mess of text, instead of a nicely formatted view.

I have tried using a richTextBox instead but got the same results.

Is there some way I can acheive this?

Cheers, Davearia. :D :D :D
 
How are you loading the XML into the TextBox? And are you sure that the document you are loading is formatted (what is the source of the document)?

You might want to consider using the XML classes that come with .Net to load and parse the XML so that you can output it formatted however you like.
 
Here is what I used in the end:
Visual Basic:
Public Sub showXML(ByVal xmlDoc As XmlDocument)
        Dim ms As System.IO.MemoryStream
        ms = FormatXMLDoc(xmlDoc)
        RichTextBox1.Text = ByteArrayToString(ms.GetBuffer)
        Me.ShowDialog()
    End Sub
    Private Function FormatXMLDoc(ByVal xmlDoc As XmlDocument) As System.IO.MemoryStream
        Dim retVal As New System.IO.MemoryStream
        Dim writer As XmlTextWriter = New XmlTextWriter(retVal, New System.Text.ASCIIEncoding)
        writer.Formatting = Formatting.Indented
        writer.Indentation = 1
        writer.IndentChar = CChar(vbTab)
        xmlDoc.WriteTo(writer)
        writer.Flush()
        writer.Close()
        Return retVal
    End Function
    Private Function ByteArrayToString(ByVal ByteArray As Byte()) As String
        Return System.Text.Encoding.UTF8.GetString(ByteArray)
    End Function

Cheers, Dave.
 
Back
Top