entweder du probierst es mal mit der Serialisierung (Beispiele siehe Projekt Demo_Serialize).
> dieses speichert aber bei Objekten nur published Properties (für "alles" Andere bietet mir Delphi keine oder nur unzureichende Informationen)
z.B. so in dieser Art:
Delphi-Quellcode:
XML := TXMLFile.Create;
Try
XML.RootNode.AddNode('
Computers').Serialize(Computers, ....);
XML.SaveToFile('
Computers.xml');
Finally
XML.Free;
End;
XML := TXMLFile.Create;
Try
XML.LoadFromFile('
Computers.xml');
XML.RootNode.AddNode('
Computers').Deserialize(Computers, ....);
Finally
XML.Free;
End;
oder du machst es selber, was aber auch nicht sooooo schwer ist
dieses ergibt dein
XML-Beispiel (nur noch mit dem Count-Node):
XML-Code:
<?
xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<xml>
<Computers>
<Count>1</Count>
<Computer>
<Name>Hallo</Name>
<LastIPByte>101</LastIPByte>
<Position>
<Left>100</Left>
<Top>25</Top>
</Position>
</Computer>
</Computers>
</
xml>
Delphi-Quellcode:
// Speichern
XML := TXMLFile.Create;
Try
XML.RootNode.AddNode('
Computers\Count').Data := Computers.Count;
For i := 0
to Computers.Count - 1
do
With XML.RootNode.AddNode('
Computers\Computer')
do Begin
AddNode('
Name').Data := Computers[i].
Name;
AddNode('
LastIPByte').Data := Computers[i].LastIPByte;
AddNode('
Position\Left').Data := Computers[i].Left;
AddNode('
Position\Top').Data := Computers[i].Top;
End;
XML.SaveToFile('
Computers.xml');
Finally
XML.Free;
End;
// laden
XML := TXMLFile.Create;
Try
XML.LoadFromFile('
Computers.xml');
Computers.Count :=
XML.RootNode.Node['
Computers\Count'].Data;
For i := 0
to Computers.Count - 1
do
With XML.RootNode.NodeList['
Computers\Computer'][i]
do Begin
Computers[i].
Name := Node['
Name'].Data;
Computers[i].LastIPByte := Node['
LastIPByte'].Data;
Computers[i].Left := Node['
Position\Left'].Data;
Computers[i].Top := Node['
Position\Top'].Data;
End;
Finally
XML.Free;
End;
und mit Attributen:
XML-Code:
<?
xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<xml>
<Computers Count="1">
<Computer Name="Hallo" LastIPByte="101" Left="100" Top="25">
</Computers>
</
xml>
Delphi-Quellcode:
// Speichern
XML := TXMLFile.Create;
Try
XML.RootNode.AddNode('
Computers').Attributes['
Count'] := Computers.Count;
For i := 0
to Computers.Count - 1
do
With XML.RootNode.AddNode('
Computers\Computer')
do Begin
Attribute['
Name'] := Computers[i].
Name;
Attribute['
LastIPByte'] := Computers[i].LastIPByte;
Attribute['
Left'] := Computers[i].Left;
Attribute['
Top'] := Computers[i].Top;
End;
XML.SaveToFile('
Computers.xml');
Finally
XML.Free;
End;
// laden
XML := TXMLFile.Create;
Try
XML.LoadFromFile('
Computers.xml');
Computers.Count :=
XML.RootNode.Node['
Computers'].Attribute['
Count'];
For i := 0
to Computers.Count - 1
do
With XML.RootNode.NodeList['
Computers\Computer'][i]
do Begin
Computers[i].
Name := Attribute['
Name'];
Computers[i].LastIPByte := Attribute['
LastIPByte'];
Computers[i].Left := Attribute['
Left'];
Computers[i].Top := Attribute['
Top'];
End;
Finally
XML.Free;
End;
man könnte auch Count weglassen und zählen:
Delphi-Quellcode:
// Speichern
XML := TXMLFile.Create;
Try
For i := 0
to Computers.Count - 1
do
With XML.RootNode.AddNode('
Computers\Computer')
do Begin
...
End;
XML.SaveToFile('
Computers.xml');
Finally
XML.Free;
End;
// laden
XML := TXMLFile.Create;
Try
XML.LoadFromFile('
Computers.xml');
Computers.Count := Length(
XML.RootNode.Node['
Computers'].NodeList['
Computer']);
For i := 0
to Computers.Count - 1
do
With XML.RootNode.NodeList['
Computers\Computer'][i]
do Begin
...
End;
Finally
XML.Free;
End;
// laden 2 (wenn es eh keine anderen Subnodes im Node "Computers" gibt)
XML := TXMLFile.Create;
Try
XML.LoadFromFile('
Computers.xml');
Computers.Count :=
XML.RootNode.Node['
Computers'].Nodes.Count;
For i := 0
to Computers.Count - 1
do
With XML.RootNode.Node['
Computers'].Node[i]
do Begin
...
End;
Finally
XML.Free;
End;
ich hoff mal, es ist jetzt nicht zu schwer?