473,725 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Inde nted;
xw.WriteStartDo cument();
xw.WriteStartEl ement("FILTER", "");
xw.WriteStartEl ement("DA");
xw.WriteString( "C:");
xw.WriteStartEl ement("DA");
xw.WriteString( "Temp");
xw.WriteEndElem ent();
xw.WriteEndElem ent();
xw.WriteEndElem ent();
xw.WriteEndDocu ment();
xw.Close();
}
System.Diagnost ics.Process.Sta rt("notepad.exe ", "C:\\Temp\\test .xml");

Produces only partially indented output:

<?xml version="1.0" encoding="utf-8"?>
<FILTER>
<DA>C:<DA>Tem p</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 3117
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.Inde nted;
* * xw.WriteStartDo cument();
* * xw.WriteStartEl ement("FILTER", "");
* * xw.WriteStartEl ement("DA");
* * xw.WriteString( "C:");
* * xw.WriteStartEl ement("DA");
* * xw.WriteString( "Temp");
* * xw.WriteEndElem ent();
* * xw.WriteEndElem ent();
* * xw.WriteEndElem ent();
* * xw.WriteEndDocu ment();
* * xw.Close();}

System.Diagnost ics.Process.Sta rt("notepad.exe ", "C:\\Temp\\test .xml");

Produces only partially indented output:

<?xml version="1.0" encoding="utf-8"?>
<FILTER>
* <DA>C:<DA>Tem p</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 XmlWriterProper ties.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.Creat e instead.
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3937
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 some computers with slow internet links this seems to be a small problem. Looks like the html page was loaded first and displayed in a very ugly layout because the css has not be loaded yet. Then the css was loaded and the page became beautiful....
3
8044
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 to check some values during that time also so I was going to be using XmlReader anyway. Unfortunately I don't see an easy way to stream it back out through XmlWriter without going node by node. Any suggestions? Is there an easier/faster way...
2
2281
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
1633
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 will be opened with Excel on client machines. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"...
0
3626
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 value (which varies depending on the data being written), characters not available in the encoding specified in the Encoding property of the XmlWritterSettings instance used to create the XmlWriter are being written to the resulting XML document as...
1
2964
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 suggestions?
9
6091
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 ss:StyleID="s87"> <ss:Data ss: Type = "String"> <Font html:Color="#FF0000">M90</Font> <Font>/Thu</Font> </ss:Data> </Cell>
12
1561
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 using XmlWriter, there is one line that I can't figure out how to generate: <ServicesCertifiedMail="OFF" DeliveryConfirmation="ON" ></Services> I've tried several different combinations of methods and can't seem to generate that string. Is it...
1
1842
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 "/>" into a new line. MS vcproj file seems to be in below format, so it should be possible ? <element1 Attr1="" <element2> Attr2=""
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.