Human-readable XML with custom indent levels

A question came in recently about how to pretty-print XML files, which normally have whitespace stripped out, but sometimes a human would like to read them.

This is very easy with TNativeXml which is included with WebHub, and is open-source within the ZaphodsMap project on sourceforge.

Sample code follows.

uses NativeXml;

procedure TForm7.Button1Click(Sender: TObject);
const
  cInputFilespec = 'D:\EducationSite\Live\WebHub\whteko\lai\WHAppConfig_lai.xml';
  cOutputFilespec = 'D:\EducationSite\Live\WebHub\whteko\lai\WHAppConfig_lai_indented.xml';
var
  xml: TUtf8NativeXml;
begin
  xml := nil;
  try
    xml := TUtf8NativeXml.Create;
    xml.LoadFromFile( cInputFilespec );
    xml.XmlFormat := xfReadable;  // makes XML human-readable
    xml.IndentString := '   ';  // whitespace used for indenting; could be a tab if you prefer
    xml.SaveToFile( cOutputFilespec );
  finally
    FreeAndNil( xml );
  end;
end;