我正在将配置数据写入 XML 文件。但是XML看起来很不结构化或者怎么说
<Configuration>
<Ftp Host="LOCALHOST" Port="21"/>
<Pop3 Host="LOCALHOST" Port="110" Interval="30000"/>
<Smtp Host="LOCALHOST" Port="25"/>
</Configuration>
我希望它看起来像
<Configuration>
<Ftp
Host="LOCALHOST"
Port="21"
/>
<Pop3
Host="LOCALHOST"
Port="110"
Interval="30000"
/>
<Smtp
Host="LOCALHOST"
Port="25"
/>
</Configuration>
有没有可能这是我的 Delphi 代码片段。我有所有类型的函数/程序,但这里只显示 2
constructor TConnXml.Create(const FileName: string);
begin
inherited Create;
fConfigfile := FileName;
fXMLDoc := TXMLDocument.Create(Application);
fXMLDoc.Options := [doNodeAutoIndent];
if FileExists(fConfigfile) then
fXMLDoc.LoadFromFile(fConfigfile)
else
begin
fXMLDoc.Active := True;
fXMLDoc.AddChild('Configuration');
fXMLDoc.SaveToFile(fConfigfile);
end;
end;
constructor TConnXml.Create;
begin
Create(SettingsFileBuild);
end;
function TConnXml.ReadString(const Section, Key, Default: string): string;
var
Node: IXMLNode;
begin
Node := fXMLDoc.DocumentElement.ChildNodes.FindNode(Section);
if Assigned(Node) and Node.HasAttribute(Key) then
Result := Node.Attributes[Key]
else
Result := Default;
end;
procedure TConnXml.WriteString(const Section, Key, Value: string);
var
Node: IXMLNode;
begin
if ReadString(Section, Key, '') = Value then
Exit;
Node := fXMLDoc.DocumentElement.ChildNodes.FindNode(Section);
if not Assigned(Node) then
Node := fXMLDoc.DocumentElement.AddChild(Section);
Node.Attributes[Key] := Value;
fModified := True;
Save;
end;
procedure TConnXml.Save;
begin
if not fModified then
Exit;
if fBackup then
CopyFile(PChar(fConfigfile), PChar(fConfigfile + '.bak'), False);
fXMLDoc.Active := True;
fXMLDoc.SaveToFile(fConfigfile);
fModified := False;
end;
function TConnXml.ReadBoolean(const Section, Key: string; Default: Boolean): Boolean;
begin
Result := Boolean(ReadInteger(Section, Key, Integer(Default)));
end;
procedure TConnXml.WriteBoolean(const Section, Key: string; Value: Boolean);
begin
WriteInteger(Section, Key, Integer(Value));
end;