使用 Delphi7 TClientDataSet:是否可以让它以缩进格式保存 XML 内容?

发布于 2024-09-26 05:19:09 字数 314 浏览 8 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

罗罗贝儿 2024-10-03 05:19:09

当您使用 TCustomClientDataSet.SaveToFile 过程时,您可以选择输出格式,默认情况下该值设置为 dfBinary ,它以二进制格式对数据进行编码。

 procedure TCustomClientDataSet.SaveToFile(const FileName: string = '';
  Format: TDataPacketFormat = dfBinary);

尝试将 Format 参数更改为 dfXMLdfXMLUTF8

ClientDataSet1.SaveToFile('file.xml',dfXML);

如果您想格式化 XML 输出,您可以使用 FormatXMLData 函数尝试此代码

uses
 XMLDoc;

Procedure FormatXMLFile(XmlFile:string);
var
   oXml : TXMLDocument;
 begin
   oXml := TXMLDocument.Create(nil);
   try
     oXml.LoadFromFile(XmlFile);
     oXml.XML.Text:=xmlDoc.FormatXMLData(oXml.XML.Text);
     oXml.Active := true;
     oXml.SaveToFile(XmlFile);
   finally
     oXml := nil;
   end;
 end;

最终您的代码将如下所示

 ClientDataSet1.SaveToFile('test.xml',dfXML);
 FormatXMLFile('test.xml');

When you uses the TCustomClientDataSet.SaveToFile procedure, you can choose the output format, for default this value is set to dfBinary wich encode the data in a binary format.

 procedure TCustomClientDataSet.SaveToFile(const FileName: string = '';
  Format: TDataPacketFormat = dfBinary);

try changing the Format parameter to dfXML or dfXMLUTF8

ClientDataSet1.SaveToFile('file.xml',dfXML);

if you want format the XML output you can use the FormatXMLData function try this code

uses
 XMLDoc;

Procedure FormatXMLFile(XmlFile:string);
var
   oXml : TXMLDocument;
 begin
   oXml := TXMLDocument.Create(nil);
   try
     oXml.LoadFromFile(XmlFile);
     oXml.XML.Text:=xmlDoc.FormatXMLData(oXml.XML.Text);
     oXml.Active := true;
     oXml.SaveToFile(XmlFile);
   finally
     oXml := nil;
   end;
 end;

finally you code will look like this

 ClientDataSet1.SaveToFile('test.xml',dfXML);
 FormatXMLFile('test.xml');
£冰雨忧蓝° 2024-10-03 05:19:09

这是因为输出文件中尚未指定正确的编码(例如 ),但它包含一些带有不兼容的编码。

作为 RRUZ 提到,在写入文件时显式指定 TDataPacketFormatdfXMLUTF8 肯定会解决“无效字符”错误,因为它将写入编码标记第一:
[...]
您还可以在文件开头为现有文件手动添加编码。

至于可读格式,一些读者可以阅读原始单行代码并为您进行格式化(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 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)

萝莉病 2024-10-03 05:19:09

我修改了你的代码,因为我在 UTF-8 方面遇到了一些问题:

Procedure FormatXMLFile(XmlFile:string);
var
   oXml : TXMLDocument;
   s : utf8String;
begin
   oXml := TXMLDocument.Create(nil);
   try
     oXml.LoadFromFile(XmlFile);
     s :=  oxml.XML.Text;
     s  := StringReplace(s, '><', '>' + #13#10 + '<' , [rfReplaceAll]);
     //oXml.XML.Text:=xmlDoc.FormatXMLData(oxml.XML.Text);
     oxml.XML.Text := s;
     oXml.Active := true;
     oXml.SaveToFile(XmlFile);
   finally
     oXml := nil;
   end;
end;

I modified your code, because I had some problems with UTF-8:

Procedure FormatXMLFile(XmlFile:string);
var
   oXml : TXMLDocument;
   s : utf8String;
begin
   oXml := TXMLDocument.Create(nil);
   try
     oXml.LoadFromFile(XmlFile);
     s :=  oxml.XML.Text;
     s  := StringReplace(s, '><', '>' + #13#10 + '<' , [rfReplaceAll]);
     //oXml.XML.Text:=xmlDoc.FormatXMLData(oxml.XML.Text);
     oxml.XML.Text := s;
     oXml.Active := true;
     oXml.SaveToFile(XmlFile);
   finally
     oXml := nil;
   end;
end;
寄与心 2024-10-03 05:19:09

dfXMLUTF8 将其用于 UTF

ClientDataSet1.SaveToFile('test.xml',dfXMLUTF8)

dfXMLUTF8 use it for UTF

ClientDataSet1.SaveToFile('test.xml',dfXMLUTF8)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文