Erika Ehrli - Adventures with Office Products & Technologies
MSDN & TechNet: Releasing Office, SharePoint, Exchange & Lync Centers and content for developers and IT professionals.

How to indent an XML file or document

How to indent an XML file or document

Rate This
  • Comments 26

Office has a deeper integration with XML technology and developers are always looking for tips and tricks to work with XML documents. Office provides support to work with XML and you might be one of those developers that is programmatically generating Word documents (using WordprocessingML) , Excel spreadsheets (using SpreadsheetML), PowerPoint slides (using PresentationML), or Visio diagrams using (DataDiagrammingML). I think it always comes handy to have a list of tips and tricks to work with XML, and today I will share with you three simple ways of indenting an XML file/document.

Indenting XML files might sound as one of those netpick or nice-to-have enhancements that you don’t really need when you are working with XML. However, lots of applications and tools generate programmatically XML files and it always comes handy to open a nice and readable indented XML file instead of a “how can I edit this!” single line of eternal XML elements.

For managed applications:

  • If you are generating XML files using and XmlTextWriter, you just need to do the following:

    [VB.NET]
    Dim writer as XmlTextWriter = new XmlTextWriter("data.xml",nothing)
    writer.Formatting 
    Formatting.Indented

    [C#]
    XmlTextWriter writer = new XmlTextWriter("data.xml",null);
    writer.Formatting Formatting.Indented;
  • If you are generating XML files/documents using DOM (XmlDocument) , you can add an XmlTextWriter to indent the code and you will be done:

    [VB.NET]
    Dim doc as XmlDocument = new XmlDocument()
    ' Save the document to a file and auto-indent the output.
    Dim writer as XmlTextWriter = new XmlTextWriter("data.xml",nothing)
    writer.Formatting 
    Formatting.Indented
    doc.Save(writer)


    [C#]
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<item><name>wrench</name></item>");
    // Save the document to a file and auto-indent the output.
    XmlTextWriter writer = new XmlTextWriter("data.xml",null);
    writer.Formatting Formatting.Indented;
    doc.Save(writer);

For any platform:

  • If you are generating XML files using an XSL transform file, you just need to add a simple line to your XSL file.

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
    <xsl:output method="xml" omit-xml-declaration="no" indent="yes" encoding="US-ASCII"/>

Happy Office XML programming!

Leave a Comment
  • Please add 1 and 6 and type the answer here:
  • Post
  • Yes, very nice Tip.
  • Hi Imi!

    It's always nice to see your comments :).
  • I was reading today an internal distribution list and I saw an interesting thread about autonumbered...
  • For some reason the indentation formatting won't work for me.  I have an XML file called GGData that I want to format.  I have the following lines of code:

    Dim doc As New XmlDocument()
    Dim writer As New XmlTextWriter("GGData.xml", Nothing)
    writer.Formatting = Formatting.Indented
    doc.Save(writer)

    The above overwrites my GGData.xml file with a blank file of 0 bytes.
  • Hi Erika

    Have you ever done the above indenting on the output of SQL Server "for xml"?

    cheers

    /s

  • fshost, you first have to load the original document into the XmlDocument instance. So put a "doc.Load("GGData.xml")" line between the "new XmlDocument()" and "Dim writer As ", and it should work...

  • Do you have any tips related to simplifying WordML(2003) documents?  

    The back story: I receive documents to be converted to pdf from the legal department in Word format (2003) and when I save the documents as pdfs the documents are often very disjointed I think because legal is tracking all the changes in the documents and to adequately track the changes it creates many nodes.  Can these documents be re-combined to be simplified again?

  • i want to fetch from Text File in connectivity from VB.NET into XML.

    Text File must be UNICODE

    IF U have any code for related to it's topic plz sent me

    my email-id :

    www.chaudhary_aparna20@yahoo.co.in

    plz send meeeeeeeeeeeeeeeeee

  • How abt for display in a TextBox?

    i tried XmlDataDocument.InnerXml but all XML in 1 line...

  • If you want it in a TextBox I assume you'd need to put the formated xml in a string.  Here's how I did it.

    public static string FormatXml(XmlDocument doc) {

    XmlTextWriter xmlWriter;

    StringWriter textWriter;

    // Format the Xml document with indentation and save it to a string.

    textWriter = new StringWriter();

    xmlWriter = new XmlTextWriter(textWriter);

    xmlWriter.Formatting = Formatting.Indented;

    doc.Save(xmlWriter);

    return textWriter.ToString();

    }

  • Works perfect. Thank you for this simple but accurate snippet!

  • Thanks for the indent code. It worked very well.

  • And for using tab as indentation char, additionally set:

    l_XMLWriter.IndentChar = '\t';

    l_XMLWriter.Indentation = 1;

    before

    l_XMLDoc.Save(l_XMLWriter);

  • &lt;p&gt;The following is an example of extracting correctly indented XML from an Xml document...&lt;/p&gt; ...

  • the .IndentChar and .Indentation properties seem only to work when you are writing it line by line, if however you do something along the lines of:

    doc.Load("C:\data.xml")

    Dim writer As New XmlTextWriter("C:\data.xml", Nothing)

    writer.Formatting = Formatting.Indented

    writer.IndentChar = Chr(9) ' ='\t'

    writer.Indentation = 3

    doc.Save("C:\data_indented.xml")

    the xml is indented, but not to the level specified, and reordering the property assignemtn doesn't seem to solve the problem... has anyone else tried this?

Page 1 of 2 (26 items) 12