问题描述
我正在使用Delphi7 TClientDataSet
读取和写入某些数据的XML文件.
I am using Delphi7 TClientDataSet
to read and write XML files for some of my data.
但是,当我想在程序外部浏览此文件时(在Windows资源管理器中双击XML),我得到了在文本内容中发现一个无效字符.错误处理资源"-即使数据可以在Delphi中正常读取和写入.
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.
是否有一种方法可以强制 TClientDataSet
以缩进方式而不是一行写入内容?
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.
无论如何:我发现使用CR/LF编写XML文件和缩进仍然更加清晰.
Anyway: I find it much clearer for an XML file to be written with CR/LF and indents anyway.
推荐答案
这是因为正确的编码(例如<?xml version ="1.0" encoding ="UTF-8"?>
)尚未在输出文件中指定,但其中包含一些编码不兼容的字符.
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.
作为,在写入文件时将 TDataPacketFormat
明确指定为 dfXMLUTF8
可以肯定会解决无效字符"错误,因为它将写入首先是编码标签:<?xml version ="1.0" encoding ="UTF-8" standalone ="yes"?>< DATAPACKET Version ="2.0"> [...]
对于已经存在的文件,您也可以在文件的开头手动添加编码.
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.
对于可读格式,某些读者可以阅读原始的一线内容并为您进行格式设置(浏览器如FireFox或Internet Exporer,以及XML编辑器如 XMLNotePad )
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)
这篇关于使用Delphi7 TClientDataSet:是否可以将XML内容以缩进格式保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!