xml.ToString with Tab and Return

How can I get the XMl formatted with Tabs and Returns ?

Using myXML.ToString

BTW: I forgot to mention that I used XMLDocument.PreserveWhiteSpace and I still get my strings without tab / return.

Also, I read the documentationpage a bit fast, skipping the first line:

When False, white space characters (spaces, carriage returns, tabs, etc.) are removed from within XML elements when the XML file is loaded. The default is False.

I skipped the most important line of the page !

However, I do not get what I wanted !

The quest continue: I found a perfect case that irritate me and remove me the desire for making a bug report:

13663 - XmlDocument.PreserveWhiteSpace has no effect
Status: Closed (By Design)

The doc says:

It has no affect when set after the XML file has been loaded.

So, how do we get:

<root> <name>Fred</name> </root>

rather than:


(from the bug report)

Hummmm. I have to explore this a little bit more.

Make a constant called IndentXML, defined as the following:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes" />
	<xsl:template match="/">
		<xsl:copy-of select="/" />

In your code, call “yourXMLDoc.Transform(IndentXML)” to get nicely formatted XML.

I stuck both of those in a module and have a routine called FormatXML that extends XMLDocument, returning the Transform.


that is a great solution!

thank you for sharing

Yes, Bill: you’re great !

In these kinds of questions I am awaiting a build-in solution i do not sa in my search. I may start to “Think Different”. ;-:slight_smile:

Nearly two years ago I seems to be happy for the answer. But, after an intensive search, I could not find where I implemented Bill answer and so I come back here for more explanation about how can I implement Extend in a Module.

If some good soul can explain the how to ?

My last test is:

Function FormatXML(Extends Format As XMLDocument) As String

<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
<xsl:output method=“xml” indent=“yes” />
<xsl:template match="/">
<xsl:copy-of select="/" />
End Function

But I got plenty errors like:

[code]Syntax error

<?xml version="1.0" encoding="UTF-8"?>[/code]

of course you are getting errors… you are attemping to “execute” XML statements
those probably need to be concatentated into a single string

Function FormatXML(Extends Format As XMLDocument) As String
dim s as string
s="<?xml version="1.0" encoding="UTF-8"?>"+endofline + _
"<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">"+endofline + _

etc.....   AND YOU WILL NEED TO ESCAPE THE DOUBLE QUOTES (or replace them with SINGLES [xml won't care]"]

return s
End Function

Thanks Dave.

I follow your advice (and double quoting each quote) and it compiles. Now, does it works ?

No, I only got the contents of the FormatXML… I knew that I do not understand something.

More to investigate.

So far you have only assembled the string that describes the transform, but you haven’t applied it to your XMLDocument yet. Read Bill Gookin’s post again; he tells you what you need to do.

Note that in the function definition you are naming the XMLDocument “Format” – maybe not the best choice.

Hi Urs,

I will do. Maybe this time (earlier in the day and after a good night) will be the good one.

Back home, I re-read Bill’s answer and yes, I misread it yesterday: I skipped the first line (at first), then…

Thanks to Bill (his original answer) and Urs for his hints.

OK: I used the Constant advice and it works fine. Now I can continue writting the Import XML method.

One more thing: for my XML debugging purposes, this is GREAT !