473,406 Members | 2,954 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,406 software developers and data experts.

Inserting a blank line between XML elements

I want to insert a white line between XML elements to raise human
readability
example:

<MyNode>
</MyNode>

<MyNode>
</MyNode>

<MyNode>
</MyNode>

How should I do it? I'm creating the XML using XmlDocument and inserting the
nodes. I tried inserting white space using for example:

xmlDoc.DocumentElement.AppendChild(xmlDoc.CreateSi gnificantWhitespace("\r\n\r\n\r\n"));

but after doing this command the formatting of the remaining XML is screwed
(there isn't any automatic end of line/indenting of the other elements of
the XML file)
Thanks!
Jun 6 '07 #1
7 15204
"MaxMax" <no**@none.comwrote in message
news:Dd*******************@twister2.libero.it...
>I want to insert a white line between XML elements to raise human
readability
example:

<MyNode>
</MyNode>

<MyNode>
</MyNode>

<MyNode>
</MyNode>

How should I do it? I'm creating the XML using XmlDocument and inserting
the nodes. I tried inserting white space using for example:
XML ignores whitespace in lots of places, so it's possible that the built in
library classes won't do what you want. You could consider running an XML
prettyprinter on the output as a seperate job, or you could write the XML as
text, handling the formatting completely (and remembering to make sure all
escapes are handled correctly). Assuming nobody comes along and tells you
how to make the library do it.
Jun 6 '07 #2
MaxMax wrote:
I want to insert a white line between XML elements to raise human
readability
example:

<MyNode>
</MyNode>

<MyNode>
</MyNode>

<MyNode>
</MyNode>

How should I do it? I'm creating the XML using XmlDocument and inserting the
nodes.
Perhaps an XmlWriter with the proper XmlWriterSettings helps e.g. this code

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.LoadXml(@"<root><foo>bar</foo><foo>bar</foo><foo>bar</foo></root>");
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.NewLineChars = Environment.NewLine +
Environment.NewLine;
using (XmlWriter xmlWriter = XmlWriter.Create(Console.Out,
xmlWriterSettings))
{
xmlDocument.Save(xmlWriter);
}

creates this output:

<root>

<foo>bar</foo>

<foo>bar</foo>

<foo>bar</foo>

</root>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 6 '07 #3
>I want to insert a white line between XML elements to raise human
>readability
example:

<MyNode>
</MyNode>

<MyNode>
</MyNode>

<MyNode>
</MyNode>

How should I do it? I'm creating the XML using XmlDocument and inserting
the nodes.

Perhaps an XmlWriter with the proper XmlWriterSettings helps e.g. this
code

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.LoadXml(@"<root><foo>bar</foo><foo>bar</foo><foo>bar</foo></root>");
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.NewLineChars = Environment.NewLine +
Environment.NewLine;
using (XmlWriter xmlWriter = XmlWriter.Create(Console.Out,
xmlWriterSettings))
{
xmlDocument.Save(xmlWriter);
}

creates this output:

<root>

<foo>bar</foo>

<foo>bar</foo>

<foo>bar</foo>

</root>
Yeah, but if you have

<foo>
<foo2>
</foo2>
</foo>

you'll obtain something TOTALLY unreadable :-)

--- bye
Jun 6 '07 #4
Hi Max,

If you want to do something about formatting of the output XML document, I
think you should focus on the XmlWriter (such as XmlTextWriter) since
XmlWriter is the underlying component that dedicated to XML document
writeout and persist. XmlDocument (DOM api class) is only responsible for
constructing XMLDocument structure rather than formatting.

Also, for add custom formatting, I have a suggestion on create a custom
XmlWriter, you can derive your custom XmlWriter class from XmlTextWriter
class.

#XmlTextWriter Members
http://msdn2.microsoft.com/en-us/lib...ter_members.as
px

For example, you can consider override one of the methods such as
"WriteEndElement" and add some additional whitespace string there. And at
runtime, whenever you want to save or writeout any XmlDocument(or directly
using XmlWriter), use your custom xmlWriter to wrapper the original writer.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 7 '07 #5
Hi Max,

Have you got any further idea on this? If there is anything else we can
help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 11 '07 #6
Have you got any further idea on this? If there is anything else we can
help, please feel free to post here.
It was too much low-priority to work on it :-) I delayed and postponed it
until "higher priority" things are ok :-)

Thanks
Jun 11 '07 #7
Thanks for your reply and glad to hear from you.

Sure. If you work on this issue later and need any further help, welcome to
discuss here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 12 '07 #8

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

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
5
by: George B | last post by:
Is it possible to use headers (H1, H2, H3 etc) but without a blank line being inserted after them ? For example, at the moment the html I have written gives me.... THIS IS THE HEADER This...
1
by: Me | last post by:
I need to test for blank inputs and run a routine "if" the input is blank. This will be used internally on a LAN to calculate selling price if the "sell" input is blank and a margin is supplied,...
3
by: BradC | last post by:
I have a big VS 2002/2003 annoyance, and wondering if there is a way to turn it off: Put your cursor at the start of an indented line of code inside a function. (right before the first...
3
by: Jeff Calico | last post by:
Hello everyone I am transforming an XML document to text, basically only outputting a small portion of it. When I run the following XSLT via Xalan's processor, I get a bunch of unwanted blank...
4
by: Steve | last post by:
Hi All, I've picked up the PyParsing module and am trying to figure out how to do a simple parsing of some HTML source code. My specific problem is dealing with an <TD></TDelement that is...
4
by: Patrick Nolan | last post by:
I am using javascript to manipulate optgroups in select elements. When the results are displayed in Firefox 2.0 there is an annoying blank line at the top of the multi-line select box. This...
5
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I...
5
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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,...
0
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...

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.