I am using Delphi7 TClientDataSet
to read and write XML files for some of my data.
However, when I want to browse this outside the program (double clicking the XML in Windows Explorer) I get the 'An invalid character was found in text content. Error processing resource' - even though the data reads and writes fine from within Delphi.
Is there a way to force TClientDataSet
to write its contents in an indented way instead of in one line?
That way I could easily open it into a text editor and find what character will trigger the above error.
Anyway: I find it much clearer for an XML file to be written with CR/LF and indents anyway.
It's because the proper encoding (like <?xml version="1.0" encoding="UTF-8"?>
) has not be specified in your output file, yet it contains some characters with an incompatible encoding.
As RRUZ mentioned, specifying explicitly the TDataPacketFormat
as dfXMLUTF8
when writing the file will most certainly solve the 'Invalid Character' error, as it will write the encoding tag first:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <DATAPACKET Version="2.0">[...]
You can also add the encoding manually at the beginning of the file for already existing files.
As for the readable formatting, some readers can read the raw one-liner and do the formatting for you (browsers like FireFox or Internet Exporer, and XML editors like XMLNotePad)