23rd March 2009

Quickly Format XML Text Files

Often when writing XML files to disk as text files developers do not include any formatting.  As such, if you need to open the file and examine it in Notepad, for example, and make changes it may be very hard to read.  There is an easy way to fix this.  You can download the executable file hereor just use the following code in Visual Studio.  NOTE: This is a console application expecting a file name as a command line argument.  You can drag your text file onto the executable file and a new file will be created with the same name as the file name with “-formatted.xml” appended.


Download the executable


Imports System.IO
ImportsSystem.Xml
Imports System.TextModuleFormatXML    
     Sub
Main(ByValArgs() As String)         
          Dim
strFile As String
          DimstrFileNew As String
          Try
         
‘ read in the file to convert from the command line
         
IfArgs.Count = 1 Then
              
strFile = Args(0)
              
IfFile.Exists(strFile) Then
                   
‘ parse the file and create a new one
                   
strFileNew = strFile & “-formatted.xml”
                   
DimstrAlldData() As String= File.ReadAllLines(strFile)
                   
DimstrData As String = String.Empty
                   
DimstrNewData AsStringBuilder = NewStringBuilder                   
                     For
EachstrData InstrAlldData
                        
‘ load the whole file into a single stringbuilder
                       
strNewData.Append(strData)
                  
Next
                    ‘ creat an XML document
                   
DimxmlDoc As NewXml.XmlDocument()
                    
‘ load the string
                   
xmlDoc.LoadXml(strNewData.ToString)
                   
‘ normalize it
                   
xmlDoc.Normalize()
                    
’save it back out
                    
xmlDoc.Save(strFileNew)                    
                    Console.WriteLine(“Done. File saved as “& strFileNew)
              
Else
                    
Console.WriteLine(“Either the file “& strFile & ” does not exist or I do not have permissions to access it.”)
              
End If
        
Else
             
Console.WriteLine(“To use this, please pass in the name of the file you want to parse.”)
        
End If
        Catch ex As Exception
            Console.WriteLine(
“Error: “ & ex.Message)
        
End Try
    
End Sub
End
Module



Using this code, an xml file that looked like “<TopLevel><MiddleLevel1><BottomLevel></BottomLevel></MiddleLevel1><MiddleLevel2></MiddleLevel2></TopLevel>” will be converted into

<TopLevel>
  <MiddleLevel1>
    <BottomLevel>
    </BottomLevel>
  </MiddleLevel1>
  <MiddleLevel2>
  </MiddleLevel2>
</TopLevel>


There are many useful features you could add to this code and make a nice product.  For example,

  • Make a UI that allows the user to select a file for input and a file for output.
  • Allow for multiple input files
  • Provide progress feedback to the user.
  • Allow scheduling
  • Quickly email the results
  • Have a preview window in the UI
This entry was posted on Monday, March 23rd, 2009 at 4:59 pm and is filed under General Software Development, Microsoft .Net. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

There are currently 3 responses to “Quickly Format XML Text Files”

Why not let us know what you think by adding your own comment! Your opinion is as valid as anyone elses, so come on... let us know what you think.

  1. 1 On October 14th, 2009, Sherry Black said:

    Very useful. Thank you.

  2. 2 On May 10th, 2010, jim said:

    Awesome. Very useful and very easy. Thanks.

  3. 3 On September 17th, 2011, web said:

    this is great

Leave a Reply

  • Links

  • Calendar

  • February 2012
    S M T W T F S
    « Jan    
     1234
    567891011
    12131415161718
    19202122232425
    26272829