NextGrid & NextDBGrid








NextGrid .NET

Export to XML from NextGrid

  • Skill:
  • Version: 1.0.0
  • Author: Phil Watkinson
  • Created: 2009-04-30
  • Updated: 2009-06-27


NextGrid already has SaveToHtml, SaveToIni, SaveToRegistry, SaveToStream and SaveToTextFile export methods which are straight forward to understand and use; but the SaveToXML method requires some explanation. The lightweight, high performance philosophy of the Next Suite means that some design compromises have been made when implementing the technical standards of XML and of Unicode encoding.

public method  SaveToXMLFile(const FileName: WideString;
  UniEncoding: TUniEncoding = enUTF_8);


TUniEncoding = (enUnspecified, enISO_8859_1, enUTF_16, enUTF_8);

and: Filename is the path and name of file to be created. If the file already exists, it is overwritten.
Let's look at the two areas of formatting grid data into XML and then the Unicode encoding in turn:
XML formatting

In the XML header, we output the required xml headers, with the unicode encoding (use the enUnspecified encoding option to avoid declaring any encoding). The string 'NxGrid' is used as the document root element and the name of the grid as the namespace. The file creation timestamp and the grid row count are added as attributes to the root element.
For the XML body the grid header captions are used as the element names; but first they are checked to ensure they conform to the XML standard, ignoring any unallowed characters found. This is where one of the design compromises exist; and to explain this, we need to understand what the XML standard says: (quote courtesy of "Professional XML 2nd Edition" by Wrox Press)
XML names cannot start with 'xml', and the initial character can only be an Unicode
letter or an underscore (_) or colon(:). The remaining chars can only be an Unicode
letter or number, or an underscore (_), a colon(:), a hypen (-) or a fullstop (.).

However, the existing code restricts the allowable character to ANSI letters, instead of Unicode letters. This means, say, that a column with the header 'École' (the French word for 'school') would have the element name of 'cole' in the xml file.

Unicode Encoding

Unicode has been with us for about ten years now, but if you are working in a English speaking country, like the author of this article, then it is still probable that you had no need nor inclination to involve yourself in the intricacies of Unicode. If so, this is a good starting point.

The default encoding is UTF-8 (UTF-8 is also used when the encoding option is enUnspecified).
In UTF-8, every code point from 0-127 is stored in a single byte. Only code points 128 and above are stored using 2, 3, in fact, up to 6 bytes. For discussion of the pros and cons of utf-8 see: It is usual practice not to provide a Byte Order Mark for uft-8 encoding, see: for further information. The Byte Order Mark for uft-16 encoding is LittleEndian.

These are sensible defaults which will be adequate in the vast majority of situations.

Source code technical notes

The code for this method is split into two parts - (1) the collation of the XML formatted data using a TMemoryStream, and then (2) the conversion into the Unicode encoding required before writing to disk.
This allows developers who are using third-party Unicode components (such as the TNT components) or Delphi 2009 to easily replace the encoding functions if required.
The TUniEncoding enumerated type is declared in NxClass.pas and is called TUniEncoding rather than TEncoding because TEncoding is used in Delphi 2009. The SaveToXML method code can be found in NxCustomGridControl.pas.

Was This Article Useful?

Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.