XML is great! It provides a very flexible data structure and means to intelligently deal with that data.

However the issue with XML i have is it's readability - it's hard to read XML document.

Consider this XML-schema code (probably not the best example - lines are truncated):
<?xml version="1.0"?>
<xs:schema xmlns:nant="http://nant.sf.net/release/0.85/nant.xsd"
elementFormDefault
="qualified" targetNamespace="http://nant.sf.net/release/0.85/nant.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:documentation>NAnt schema generated at 10/14/2006 16:12:51</xs:documentation>
</xs:annotation>
<xs:complexType name="NAnt.Core.TaskContainer">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="asminfo" type="nant:NAnt.DotNet.Tasks.AssemblyInfoTask" />
<xs:element name="al" type="nant:NAnt.DotNet.Tasks.AssemblyLinkerTask" />
<xs:element name="csc" type="nant:NAnt.DotNet.Tasks.CscTask" />
<xs:element name="delay-sign" type="nant:NAnt.DotNet.Tasks.DelaySignTask" />
<xs:element name="ilasm" type="nant:NAnt.DotNet.Tasks.IlasmTask" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
It's not that easy to read is it? I found that it's just a matter of putting spaces, line breaks and indentation in the right places to make it easier to read:
<?xml version = "1.0"?>
<xs:schema
xmlns:nant = "http://nant.sf.net/release/0.85/nant.xsd"
elementFormDefault = "qualified"
targetNamespace = "http://nant.sf.net/release/0.85/nant.xsd"
xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:annotation>
<xs:documentation>
NAnt schema generated at 10/14/2006 16:12:51
</xs:documentation>
</xs:annotation>
<xs:complexType
name = "NAnt.Core.TaskContainer">
<xs:sequence
minOccurs = "0"
maxOccurs = "unbounded">
<xs:choice
minOccurs = "0"
maxOccurs = "unbounded">
<xs:element
name = "asminfo"
type = "nant:NAnt.DotNet.Tasks.AssemblyInfoTask" />
<xs:element
name = "al"
type = "nant:NAnt.DotNet.Tasks.AssemblyLinkerTask" />
<xs:element
name = "csc"
type = "nant:NAnt.DotNet.Tasks.CscTask" />
<xs:element
name = "delay-sign"
type = "nant:NAnt.DotNet.Tasks.DelaySignTask" />
<xs:element
name = "ilasm"
type = "nant:NAnt.DotNet.Tasks.IlasmTask" />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I moved each attribute to a separate line and added spaces around equal sign "=". Looks more readable doesn't it?

Two issues with this layout, though:
  1. It look much longer - nothing to worry about i would say.
  2. Text notes (e.g. <xs:documentation>)  will have extra spaces and line breaks in it's value. So it's better to leave as:
<xs:documentation>NAnt schema generated at 10/14/2006 16:12:51</xs:documentation>
I would actually expect this kind of formatting option of XML formatting from all XML editor (especially from Visual Studio).
Tags: