By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,609 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,609 IT Pros & Developers. It's quick & easy.

XML in a Textbox

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies

P: n/a
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


"Adrian" <ad****@nospam.comk> wrote in message
news:db**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
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

Nov 21 '05 #2

P: n/a
> 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 News: It gives a nicely formatted output for my textbox
Bad news: 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.GetB uffer())

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

Nov 21 '05 #3

P: n/a
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
<al*******@users.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
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 News: It gives a nicely formatted output for my textbox
Bad news: 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.GetB uffer())

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

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.