简体   繁体   English

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

[英]Using Delphi7 TClientDataSet: is it possible to have it save its XML contents in an indented format?

I am using Delphi7 TClientDataSet to read and write XML files for some of my data.我正在使用 Delphi7 TClientDataSet为我的一些数据读取和写入 XML 文件。

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.但是,当我想在程序外浏览它(在 Windows 资源管理器中双击 XML)时,我收到“在文本内容中发现无效字符”。 Error processing resource' - even though the data reads and writes fine from within Delphi.错误处理资源' - 即使数据在 Delphi 中读写正常。

Is there a way to force TClientDataSet to write its contents in an indented way instead of in one line?有没有办法强制TClientDataSet以缩进的方式而不是一行来写入其内容?

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.无论如何:我发现无论如何用 CR/LF 和缩进编写 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.当您使用TCustomClientDataSet.SaveToFile过程时,您可以选择输出格式,默认情况下,此值设置为dfBinary ,以二进制格式对数据进行编码。

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

try changing the Format parameter to dfXML or dfXMLUTF8尝试将Format参数更改为dfXMLdfXMLUTF8

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

if you want format the XML output you can use the FormatXMLData function try this code如果你想格式化 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;

finally you code will look like this最后你的代码看起来像这样

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

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.这是因为您的输出文件中尚未指定正确的编码(如<?xml version="1.0" encoding="UTF-8"?> ),但它包含一些编码不兼容的字符。

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:正如RRUZ 所提到的,在写入文件时明确指定TDataPacketFormatdfXMLUTF8肯定会解决“无效字符”错误,因为它将首先写入编码标签:
<?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 )至于可读格式,一些读者可以读取原始一行代码并执行格式化你(浏览器如Firefox或Internet Explorer的,像XML编辑器XMLNotePad

I modified your code, because I had some problems with UTF-8:我修改了您的代码,因为我在使用 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;

dfXMLUTF8 use it for UTF dfXMLUTF8 将其用于 UTF

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM