How to reformat XML programmatically?

I have an XML input document that is horribly formatted (this is a Delphi project file if someone cares) - inconsistent indentation, empty lines, node lines grouped together:

<BorlandProject><Delphi.Personality><Parameters><Parameters Name="HostApplication">C:\Some\Path\Filename.exe</Parameters> <!--etc--> <Excluded_Packages> </Excluded_Packages> 

I want to reformat it into something nice. What is an easy way to do this programmatically, with Win32 / COM? If MSXML, how do I do this?

I would also like to specify the indentation block (tab / multiple spaces).

I tried using the Delphi MSXML wrapper TXmlDocument, and it really removes empty lines and tabbed indents, but does not split lines like this:

 <BorlandProject><Delphi.Personality><Parameters><Parameters Name="HostApplication">C:\Some\Path\Filename.exe</Parameters> <!--etc--> <Excluded_Packages> 
+4
source share
1 answer

I tested the FormatXMLData function in the delphi project file and it works fine, it printed all the lines correctly.

check this code.

 uses XMLIntf, XMLDoc; Procedure FormatXMLFile(const XmlFile:string); var oXml : IXMLDocument; begin oXml := TXMLDocument.Create(nil); oXml.LoadFromFile(XmlFile); oXml.XML.Text:=xmlDoc.FormatXMLData(oXml.XML.Text); oXml.Active := true; oXml.SaveToFile(XmlFile); end; 
+15
source

All Articles