How to use XmlTextWriter formatting in C#

You can create a XML document, by using the basic XmlTextWriter class. If you want to understand how you can read the  article How to use XMLTextWriter to create an XML document in C#. By default, the XmlTextWriter creates an XML file that has all its elements lumped together in a single line without any helpful carriage returns or indentation. If you open the file with Microsoft Internet Explorer you don’t see this limitation because Internet Explorer uses a style sheet to give the XML a more readable (and more colorful) appearance. However, if you open the XML document in Notepad, you can see the difference.

Although additional formatting isn’t required, it can make a significant difference if you want to read your XML files in Visual Studio, Notepad, or another text editor. Fortunately, the XmlTextWriter supports formatting; you just need to enable it, as follows:

 

// Set it to indent output.
w.Formatting = Formatting.Indented;

 

// Set the number of indent spaces.
w.Indentation = 5;