468,133 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

Indenting XML output?

I'm having a problem getting XML output properly formatted. specifically,
line breaks and indentation are missing, even though I'm using an
XmlTextWriter with Formatting = Formatting.Indented.

Here is the code that's generating the problem:

static void Main(string[] args)
// Create an XML document
XmlDocument document = new XmlDocument();

// Add an XML declaration section
XmlNode newNode = document.CreateNode(XmlNodeType.XmlDeclaration,"", "");

// Add a root element
XmlElement newElement = document.CreateElement("DemoXmlDocument");
XmlText newElementText = document.CreateTextNode("This is the text of
the root element");

// Get the root element
XmlElement rootElement = document.DocumentElement;

// Add a child element
newElement = document.CreateElement("SampleElement");
newElementText = document.CreateTextNode("The text of the sample

// Save the XML document to file
string fileName = @"c:\temp\DemoDocument.xml";
XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.Default);
writer.Formatting = Formatting.Indented;

Here's what the output from the program looks like:

<?xml version="1.0"?>
<DemoXmlDocument>This is the text of the root element<SampleElement>The text
of the sample element</SampleElement></DemoXmlDocument>

The content is fine, but the formatting is a mess. Any ideas why? Thanks for
your help.

David Veeneman
Foresight Systems
Oct 4 '06 #1
3 3797
I found my answer. The XML formatter was being thrown off by the fact that I
had included a text attribute in my root element. So, I changed my root
element to the following:

// Add a root element
XmlElement newElement = document.CreateElement("DemoXmlDocument");

The document now formats correctly.

David Veeneman
Foresight Systems
Oct 4 '06 #2
This will work aswell... using the XmlTextWriter.Formatting =
Maybe easier
public static void WriteIndentedXml(string xml)
XmlDocument doc = new XmlDocument();
catch (Exception ex)

System.IO.StringWriter stringWriter = new System.IO.StringWriter();
XmlTextWriter writer = new XmlTextWriter(stringWriter);
writer.Formatting = Formatting.Indented;


Oct 5 '06 #3
I did find an answer to this--the problen was that I was adding element text
to the root element, which was confusing the XML formatter. Elements that
are designed to contain other elements shouldn't get element text.

David Veeneman
Foresight Systems
Oct 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by David Ehmer | last post: by
1 post views Thread by Yama | last post: by
5 posts views Thread by Jean Pion | last post: by
7 posts views Thread by Harrie | last post: by
reply views Thread by D. Shane Fowlkes | last post: by
3 posts views Thread by akashkurdekar | last post: by
4 posts views Thread by Christopher | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.