Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language

Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

Inserting a blank line between XML elements

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




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.CreateSignificantWhitespace("\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)


"MaxMax" <n@none.com> wrote in message


>I want to insert a white line between XML elements to raise human
> 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.


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></roo t>");
             XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
             xmlWriterSettings.Indent = true;
             xmlWriterSettings.NewLineChars = Environment.NewLine +
             using (XmlWriter xmlWriter = XmlWriter.Create(Console.Out,

creates this output:







        Martin Honnen --- MVP XML


Yeah, but if you have


you'll obtain something TOTALLY unreadable :-)

--- bye


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

#XmlTextWriter Members

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.


Steven Cheng

Microsoft MSDN Online Support Lead


Get notification to my posts through email? Please refer to

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


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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc