XMLWriter ugly formatting.

J

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
 
P

Pavel Minaev

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.
 

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

Top