Inserting a blank line between XML elements

  • Thread starter Thread starter MaxMax
  • Start date Start date
M

MaxMax

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.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)


Thanks!
 
MaxMax said:
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.
 
MaxMax said:
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>
 
I want to insert a white line between XML elements to raise human
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
 
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/library/system.xml.xmltextwriter_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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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


This posting is provided "AS IS" with no warranties, and confers no rights.
 
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.
 
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
 
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.
 
Back
Top