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

XMLWriter ugly formatting.

P: n/a
JAM
I'm trying to code some directory structure as my output / input file
using XML. I would like to see formatiing with indentations. mimicking
directory tree structure. Unfortunately the code such as this:

using (XmlTextWriter xw = new XmlTextWriter("C:\\Temp\\test.xml",
Encoding.UTF8))
{
xw.Formatting = Formatting.Indented;
xw.WriteStartDocument();
xw.WriteStartElement("FILTER", "");
xw.WriteStartElement("DA");
xw.WriteString("C:");
xw.WriteStartElement("DA");
xw.WriteString("Temp");
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Close();
}
System.Diagnostics.Process.Start("notepad.exe", "C:\\Temp\\test.xml");

Produces only partially indented output:

<?xml version="1.0" encoding="utf-8"?>
<FILTER>
<DA>C:<DA>Temp</DA></DA>
</FILTER>

Where obviously my intent was to get:

<?xml version="1.0" encoding="utf-8"?>
<FILTER>
<DA>
C:
<DA>
Temp
</DA>
</DA>
</FILTER>

(where Temp is a subdirectory of the drive C:)

Obviously the key here is to keep directory tree structure expressed
in the XML file.

Take for example more complicated case. Let's say you have a following
directory structure:

A
B
C
D
E
F
G

In other words: root directory A has three subdirectories: B, C and F.
From those three C has in turn it's own subdirectories D and E, and F
has one subdirectory G.

The XML structure that I imagined should look like this:

<DA>
A
<DA>
B
</DA>
<DA>
C
<DA>
D
</DA>
<DA>
E
</DA>
</DA>
<DA>
F
<DA>
G
</DA>
</DA>
</DA>

Where just by looking at indentations you could deduct visually the
tree structure. It also easy to generate this code using recursive
function that travels directory tree. Unfortunately the code I have
generates this:

<DA>A<DA>B</DA><DA>C<DA>D</DA><DA>E</DA></DA><DA>F<DA>G</DA></DA></DA>

The above has basically the same information stored as the formatted
one but it misses visual feedback that is usefull for debuging, etc.
When I manually create XML inside Visual Studio it does generates nice
feedback with indentations.

How can I do that easily with XMLTextWriter ? Of course I'm looking
for a simple solution. Otherwise using XMLTextWriter defies the
simplicity and I would rather change the output format to proprtiary
with my own procedures instead of using XML all together.

JAM
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Jun 25, 5:52*pm, JAM <ja_1...@yahoo.comwrote:
I'm trying to code some directory structure as my output / input file
using XML. I would like to see formatiing with indentations. mimicking
directory tree structure. Unfortunately the code such as this:

using (XmlTextWriter xw = new XmlTextWriter("C:\\Temp\\test.xml",
Encoding.UTF8))
{
* * xw.Formatting = Formatting.Indented;
* * xw.WriteStartDocument();
* * xw.WriteStartElement("FILTER", "");
* * xw.WriteStartElement("DA");
* * xw.WriteString("C:");
* * xw.WriteStartElement("DA");
* * xw.WriteString("Temp");
* * xw.WriteEndElement();
* * xw.WriteEndElement();
* * xw.WriteEndElement();
* * xw.WriteEndDocument();
* * xw.Close();}

System.Diagnostics.Process.Start("notepad.exe", "C:\\Temp\\test.xml");

Produces only partially indented output:

<?xml version="1.0" encoding="utf-8"?>
<FILTER>
* <DA>C:<DA>Temp</DA></DA>
</FILTER>

*Where obviously my intent was to get:

<?xml version="1.0" encoding="utf-8"?>
<FILTER>
* <DA>
* * C:
* * <DA>
* * * Temp
* * </DA>
* </DA>
</FILTER>

(where Temp is a subdirectory of the drive C:)
The problem here is that your second example does not represent the
XML you've outputted - if you read it using XmlReader, you'll notice
that you won't read back plain "C:", but instead it will have some
leading and trailing whitespace. This is due to the nature of
whitespace handling in XML - read the XML specification for more
information. In this case, XmlWriter just did precisely what you asked
it to do - namely, to generate a file which contains string "C:", with
no whitespace at the beginning or at the end. This is actually covered
by MSDN, in documentation for XmlWriterProperties.Indent property:

"The elements are indented as long as the element does not contain
mixed content. Once the WriteString or WriteWhitespace method is
called to write out a mixed element content, the XmlWriter stops
indenting. The indenting resumes once the mixed content element is
closed."

By the way, do not use XmlTextWriter - it's deprecated. Use
XmlWriter.Create instead.
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.