XML in a Textbox

  • Thread starter Thread starter Adrian
  • Start date Start date
A

Adrian

Hi,
I have an xml doc in a string it all just runs to gether no crlf or
indents, I want to lay it out in a textbox so each element is on its
ownline and indented correctly.

how do I do this

vb dotnet

thanks
 
July 22, 2005

Check into XML Readers and scroll through the elements and outputing them
gradually into the text box.... it is going to be a lot of work...

--
Joseph Bittman
Microsoft Certified Application Developer

Web Site: http://71.39.42.23
Static IP
 
I have an xml doc in a string it all just runs to gether no crlf or
indents, I want to lay it out in a textbox so each element is on its
ownline and indented correctly.


The following routine creates a string from an XML document with all
the formatting characters (crlf, tab et al.) in place.

Good It gives a nicely formatted output for my textbox
Bad There a few strangenesses with it that I haven't tracked down
yet and might cause problems (or they might not)


The basic idea is to use an XMLTextWriter to write to a MemoryStream
and then read the contents of the memStream into a string.


Private Function PrettyPrintXML(ByVal xmlDoc As XmlDocument) As
String

Dim ret As String = String.Empty

Dim memStream As New MemoryStream
Dim writer As XmlTextWriter
Dim reader As StreamReader

Try

writer = New XmlTextWriter(memStream,
System.Text.Encoding.UTF8)

writer.Formatting = Formatting.Indented
writer.Indentation = 3
xmlDoc.WriteTo(writer)
writer.Flush()

' NOTE1 : We can't close the writer here as this will close
the memstream

'ret =
System.Text.Encoding.UTF8.GetString(memStream.GetBuffer())

'NOTE2: This looks like it should work but gives me extra
characters at the start of the
' string. (1st 3 chars of the memstream are not
obviously part of the XML output.

memStream.Seek(0, SeekOrigin.Begin)
reader = New StreamReader(memStream)
ret = reader.ReadToEnd


Finally

'NOTE3: As both the reader and writer are pointing to the
memstream and both
' try to close the memstream when they are
closed we cannot close both
' No idea if this will cause an error or not.

reader.Close()

End Try

Return ret

End Function


If anyone can come up with a better way (or tweaks on this code) I
would be interested.

Thanks
Alan.
 
Hi Allan
thanks for the approach and code sample re the 1st and third char not be
correct I either don't understand this or its not a problem on the XML I'm
looking at...

It work great :)

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top