473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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,"", "");
document.AppendChild(newNode);

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

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

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

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

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 4235
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");
document.AppendChild(newElement);

The document now formats correctly.

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

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

Console.WriteLine(stringWriter.ToString());
}

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hyunchan Kim | last post by:
To indent xml file, I made an instance of Transformer from Templates using following. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...
0
by: David Ehmer | last post by:
Appreciate any ideas about what might be happening with these pages in IE5.2 Mac. In summary the 2nd paragraph or list item is indenting to the right. paragraphs of list items after the 2nd...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
1
by: Yama | last post by:
Hi, How do you indent the output of a form? Whenever I have a form I get this type of output: <form name="frmAboutOverview" method="post" action="AboutOverview.aspx"...
5
by: Jean Pion | last post by:
Dear readers, If you use some funtion to generate html code like: <? function display_stuff( parameters) { echo "<p>\n"; echo " <table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"...
7
by: Harrie | last post by:
Hi group, I want to indent existing XML files so they are more readable (at least to me). At this moment I'm looking at the XML files OpenOffice.org's Writer application produces in it's zipped...
0
by: D. Shane Fowlkes | last post by:
The Smart Indenting "feature" is driving me absolutlely crazy in VWD. Problem: VWD in Code View insists on tabbing a lot of my code over to it's own liking and effects mainly my comments in the...
3
by: akashkurdekar | last post by:
Sorry if this seems like a newbie question, because I *am* rather new to XML I have a string with XML content string s = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><Service...
4
by: Christopher | last post by:
How would I go about indenenting for each level of recursion, if I am trying to output the contents of a class which contains its own type? where AttributeGroupMap is typdef...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.