By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Inserting a blank line between XML elements

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
"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

P: n/a
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

P: n/a
>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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.