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
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" <n@none.com> wrote in message

news:Ddx9i.30398$%k.130268@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.

-----------------------------------------------Reply-----------------------------------------------

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 +
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/

-----------------------------------------------Reply-----------------------------------------------

Yeah, but if you have

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

you'll obtain something TOTALLY unreadable :-)

--- bye

-----------------------------------------------Reply-----------------------------------------------

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_mem...
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.asp...
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.

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