XmlDocument and the removal of the indentations. It is good.

Imagine that you have a program that creates XML documents using the XmlDocument class. See:

".NET Framework Class Library - XmlDocument Class"


You do this by creating and adding Elements to the document.

The source could be, for example, data from a database.

Now, when checking the output, you notice that the generated XML looks something like this:



    <Author>Author Number: 0</Author>

    <Title>The Book on number: 0</Title>

    <Year>Year: 0</Year>

    <ISBN>ISBN 123-0000</ISBN>

    <Category>Category: 0</Category>



However, you wish for it to look like this:

<MyBooks><Book><Author>Author Number: 0</Author><Title>The Book on number: 0</Title><Year>Year: 0</Year><ISBN>ISBN 123-0000</ISBN><Category>Category: 0</Category></Book></MyBooks>

In other words, you wish the output to not contain the indentations.

Why would you wish to do that you may ask, it is so good for readability. This is true. But all unnecessary whitespace adds to the file size.

Now you say that disk is cheap and this should not cause any issues.

However, in most cases XML is not read by humans, instead it is used as a simple way to transport data.

And when transporting data the way the XML looks is of no interest.

Also, if it is to be presented to humans, there is usually an interpreter that parses the XML and makes it readable or to fill up a data grid or something similar.

So for this reason the removal of the indentations is desirable, so how do you do it?

The answer is simple but, to me at least, a little bit counterintuitive. You simply set XmlDocument.PreserveWhitespace property to True.

By counterintuitive I mean that by setting this to true, you get the impression that the indentations (ie. whitespace) is preserved and therefore

they will still be there. But no, reading the documentation you’ll see the following:

“If PreserveWhitespace is true before Load or LoadXml is called, white space nodes are preserved; otherwise, if this property is false, significant white space is preserved, white space is not.

If PreserveWhitespace is true before Save is called, white space in the document is preserved in the output; otherwise, if this property is false, XmlDocument auto-indents the output.“


".NET Framework Class Library - XmlDocument.PreserveWhitespace Property"


So the short answer to remove indentations is: doc.PreserveWhitespace = true;

A short example of this:

        static void Main(string[] args)


            string fileName = "C:\xmlOutput.xml";

            XmlDocument doc = new XmlDocument();

            // Here you have to set PreserveWhitespace to true, if not, you will have the indentation.

            // So uncommet the line, and the indentation will go away.

            //doc.PreserveWhitespace = true;

            XmlElement root = doc.CreateElement("MyBooks");


            for (int i = 0; i < 1; i++)


                XmlElement xeBook = doc.CreateElement("Book");

                XmlElement xeAuthor = doc.CreateElement("Author");

                XmlElement xeTitle = doc.CreateElement("Title");

                XmlElement xeYear = doc.CreateElement("Year");

                XmlElement xeIsbn = doc.CreateElement("ISBN");

                XmlElement xeCategory = doc.CreateElement("Category");


                xeAuthor.InnerText = String.Format("Author Number: {0}", i.ToString());



                xeTitle.InnerText = String.Format("The Book on number: {0}", i.ToString());


                xeYear.InnerText = String.Format("Year: {0}", i.ToString());


                xeIsbn.InnerText = String.Format("ISBN 123-000{0}", i.ToString());


                xeCategory.InnerText = String.Format("Category: {0}", i.ToString());





Run it, and you will get the indentations, uncomment the PreserveWhitespace and you don’t.

As an example, if you change the number of iterations to 5000, file size for the file without the indentations is around 20% less than with the indentations.

Obviously the example above is fictional, but in a real scenario where you transport a lot of XML data across networks (which may be slow in itself) then 20% is quite a lot.