使用 Delphi7 TClientDataSet:是否可以让它以缩进格式保存 XML 内容?
我正在使用 Delphi7 TClientDataSet
来读取和写入一些数据的 XML 文件。
但是,当我想在程序外部浏览此内容(在 Windows 资源管理器中双击 XML)时,我得到“在文本内容中发现无效字符”。错误处理资源' - 即使数据在 Delphi 中读取和写入正常。
有没有办法强制 TClientDataSet
以缩进方式而不是一行写入其内容?
这样我就可以轻松地将其打开到文本编辑器中并找到哪个字符会触发上述错误。
无论如何:我发现使用 CR/LF 和缩进来编写 XML 文件会更清晰。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您使用
TCustomClientDataSet.SaveToFile
过程时,您可以选择输出格式,默认情况下该值设置为 dfBinary ,它以二进制格式对数据进行编码。尝试将
Format
参数更改为dfXML
或dfXMLUTF8
如果您想格式化 XML 输出,您可以使用
FormatXMLData
函数尝试此代码最终您的代码将如下所示
When you uses the
TCustomClientDataSet.SaveToFile
procedure, you can choose the output format, for default this value is set todfBinary
wich encode the data in a binary format.try changing the
Format
parameter todfXML
ordfXMLUTF8
if you want format the XML output you can use the
FormatXMLData
function try this codefinally you code will look like this
这是因为输出文件中尚未指定正确的编码(例如
),但它包含一些带有不兼容的编码。
作为 RRUZ 提到,在写入文件时显式指定
[...]
TDataPacketFormat
为dfXMLUTF8
肯定会解决“无效字符”错误,因为它将写入编码标记第一:您还可以在文件开头为现有文件手动添加编码。
至于可读格式,一些读者可以阅读原始单行代码并为您进行格式化(FireFox 或 Internet Exporer 等浏览器,以及 XMLNotePad)
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
asdfXMLUTF8
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)
我修改了你的代码,因为我在 UTF-8 方面遇到了一些问题:
I modified your code, because I had some problems with UTF-8:
dfXMLUTF8 将其用于 UTF
ClientDataSet1.SaveToFile('test.xml',dfXMLUTF8)
dfXMLUTF8 use it for UTF
ClientDataSet1.SaveToFile('test.xml',dfXMLUTF8)