473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

XMLWriter ugly formatting.

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
1 3076
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Quick Function | last post by:
Hi, I developed a site and used css. I put almost all style information in the css and used a lot of "id=my_css_class" in the html. They is little style specification in the html. I found that on...
3
by: Michael Malinak | last post by:
Since XmlWriter offers so many nice options for formatting, I thought it would be nice to read in via XmlReader, and write back out via XmlWriter. It might be overkill, but I'd also like to be able...
2
by: Harry | last post by:
Hi, I am using XMLWriter to build xml and I need to build the element below. How do I do this? <Password format="encrypted">password</Password> Thanks
1
by: Paul | last post by:
Hi I am trying to create the attributes shown below using the xmlwriter, just wondering if anyone has any ideas how to accomplish this? I am creating a xml file in a dot.net web application that...
0
by: Janusz Nykiel | last post by:
I've stumbled upon unexpected behavior of the .NET 2.0 System.Xml.XmlWriter class when using it to write data to a binary stream (System.IO.Stream). If the amount of data is less than a certain...
1
by: MAF | last post by:
Is there a simple way to get the text, xml, from an xmlwriter. I have a XML writer that writes to a file, and I want another function to return the text that the xml writer is produced. Any...
9
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a .net application and am using the xml writer class to create an xml file that opens as an excel file. I am trying to write out the following but am having difficulty. <Row> <Cell...
12
by: sklett | last post by:
**Disclaimer** The element I need to create might not be valid XML, but it's not my fault, I'm following a required format for an integration project. I've got 99% of the XML created nicely...
1
by: Mikey_L | last post by:
hi is it possible to make XmlWriter output with formatting like below ? I have gotten it working mostly (with XmlWriterSetting.Indent=true). However, I can not find a way to make ">" or "/>"...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.