Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#7

AW: XML Einstieg - wie? einfach? Stringgrid ins XML und zurück!

  Alt 9. Okt 2012, 12:30
Es sind beliebig viele Arten der Implementierung möglich, eine der einfachsten dürfte das hier sein ...

Delphi-Quellcode:
Procedure SaveStringGrid2XML(sg:TStringGrid;const fn:String);
//2012 bummi
var
  C, r: Integer;
  row: IXMLNode;
  I: IXMLDocument;
begin
  I := TXMLDocument.Create(nil);
  I.Active := True;
  I.DocumentElement := I.CreateNode('DATA', ntElement, '');
  for r := 0 to sg.RowCount - 1 do
  begin
    row := I.DocumentElement.AddChild('ROW');
    for C := 0 to sg.ColCount - 1 do
    begin
      row.Attributes['COL'+IntToStr(C)] := sg.Cells[c,r];
    end;
  end;
  I.SaveToFile(fn)
end;



Procedure LoadStringGridFromXML(sg:TStringGrid;Const fn:String);
//2012 bummi
var
  I: IXMLDocument;
  row: IXMLNode;
  r,c:Integer;
  Procedure SetColCount;
    begin
       if sg.ColCount<>row.AttributeNodes.Count then sg.ColCount := row.AttributeNodes.Count;
    end;
begin
  I := TXMLDocument.Create(fn);
  I.Active := true;
  I.DocumentElement := i.ChildNodes[0];
  sg.RowCount := I.DocumentElement.ChildNodes.Count;
  for r := 0 to I.DocumentElement.ChildNodes.Count-1 do
    begin
       row := I.DocumentElement.ChildNodes[r];
       if r=0 then SetColCount;
       for c := 0 to row.AttributeNodes.Count - 1 do
          begin
            //sg.Cells[c,r] := row.Attributes['COL'+IntToStr(C)];
            sg.Cells[c,r] := row.AttributeNodes.Get(c).Text;
          end;
    end;
end;
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat