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