XML操作

procedure TForm1.btnDat2XmlClick(Sender: TObject);
var
  XMLDoc: TXMLDocument;
  Node1: IXMLNode;
  Node2: IXMLNode;
  MyStream: TMemoryStream;
  i: integer;
begin
  
if FileExists('.\GoodsSale_Tw.dat'then begin
    XMLDoc :
= TXMLDocument.Create(nil);
    XMLDoc.Active :
= True;
    XMLDoc.Version :
= '1.0';
    XMLDoc.Encoding :
= 'GB2312';
    XMLDoc.Options :
= [doNodeAutoCreate, doNodeAutoIndent, doAttrNull, doAutoPrefix, doNamespaceDecl];
    XMLDoc.DocumentElement :
= XMLDoc.CreateNode('table');
    myStream :
= TMemoryStream.Create;
    myStream.LoadFromFile(
'.\GoodsSale_Tw.dat');
    Count :
= (myStream.Size - 40div 10;
    
//myStream.ReadBuffer(Head[0], 10);
    MyStream.Position :
= 10;
    
for i := 0 to Count - 1 do
    
begin
      new(Pgshop);
      MyStream.Read(Pgshop^, sizeof(gshop_info));
      Node1 :
= XMLDoc.DocumentElement;
      Node2 :
= Node1.AddChild('row');
      
if ((i + 1mod 2<> 0 then begin
        Node2.SetAttributeNS(
'大类''', Pgshop^.a1 xor $72);
        Node2.SetAttributeNS(
'位置''', Pgshop^.a2 xor $67);
        Node2.SetAttributeNS(
'物品ID''', Pgshop^.a3 xor $AA13);
        Node2.SetAttributeNS(
'折叠数量''', Pgshop^.a5 xor $86);
        Node2.SetAttributeNS(
'价格''', Pgshop^.a6 xor $DFB0);
        Node2.SetAttributeNS(
'功能1''', Pgshop^.a8 xor $C2);
        Node2.SetAttributeNS(
'功能2''', Pgshop^.a9 xor $C8);
        Node2.SetAttributeNS(
'标识''', Pgshop^.a10 xor $9D);
      
end else begin
        Node2.SetAttributeNS(
'大类''', Pgshop^.a1 xor $4E);
        Node2.SetAttributeNS(
'位置''', Pgshop^.a2 xor $B0);
        Node2.SetAttributeNS(
'物品ID''', Pgshop^.a3 xor $D103);
        Node2.SetAttributeNS(
'折叠数量''', Pgshop^.a5 xor $BC);
        Node2.SetAttributeNS(
'价格''', Pgshop^.a6 xor $8E05);
        Node2.SetAttributeNS(
'功能1''', Pgshop^.a8 xor $07);
        Node2.SetAttributeNS(
'功能2''', Pgshop^.a9 xor $92);
        Node2.SetAttributeNS(
'标识''', Pgshop^.a10 xor $80);
      
end;
    
end;
    
//myStream.ReadBuffer(LastRecord, 10);
    
//myStream.ReadBuffer(Bottom[0], 20);
    XMLDoc.SaveToFile(
'.\GoodsSale_Tw.xml');
    XMLDoc.Free;
    myStream.Free;
  
end;
end;

procedure TForm1.btnXml2DatClick(Sender: TObject);
var
  XMLDoc: TXMLDocument;
  Node, Node1: IXMLNode;
  MyStream: TMemoryStream;
  i, Count: integer;
begin
  
if FileExists('.\GoodsSale_Tw.xml'then begin
    XMLDoc :
= TXMLDocument.Create(Application);
    XMLDoc.LoadFromFile(
'.\GoodsSale_Tw.xml');
    XMLDoc.Active :
= True;
    Node :
= XMLDoc.DocumentElement;
    Count :
= Node.ChildNodes.Count;
    myStream :
= TMemoryStream.Create;
    myStream.Write(Head[
0], Length(Head));
    
for i := 0 to Count - 1 do begin
      Node1 :
= XMLDoc.DocumentElement.ChildNodes[i];
      new(Pgshop);
      
if ((i + 1mod 2<> 0 then begin
        Pgshop^.a1 :
= Byte(StrToInt(Node1.Attributes['大类'])) xor $72;
        Pgshop^.a2 :
= Byte(StrToInt(Node1.Attributes['位置'])) xor $67;
        Pgshop^.a3 :
= StrToInt(Node1.Attributes['物品ID']) xor $AA13;
        Pgshop^.a5 :
= Byte(StrToInt(Node1.Attributes['折叠数量'])) xor $86;
        Pgshop^.a6 :
= StrToInt(Node1.Attributes['价格']) xor $DFB0;
        Pgshop^.a8 :
= Byte(StrToInt(Node1.Attributes['功能1'])) xor $C2;
        Pgshop^.a9 :
= Byte(StrToInt(Node1.Attributes['功能2'])) xor $C8;
        Pgshop^.a10 :
= Byte(StrToInt(Node1.Attributes['标识'])) xor $9D;
      
end else begin
        Pgshop^.a1 :
= Byte(StrToInt(Node1.Attributes['大类'])) xor $4E;
        Pgshop^.a2 :
= Byte(StrToInt(Node1.Attributes['位置'])) xor $B0;
        Pgshop^.a3 :
= StrToInt(Node1.Attributes['物品ID']) xor $D103;
        Pgshop^.a5 :
= Byte(StrToInt(Node1.Attributes['折叠数量'])) xor $BC;
        Pgshop^.a6 :
= StrToInt(Node1.Attributes['价格']) xor $8E05;
        Pgshop^.a8 :
= Byte(StrToInt(Node1.Attributes['功能1'])) xor $07;
        Pgshop^.a9 :
= Byte(StrToInt(Node1.Attributes['功能2'])) xor $92;
        Pgshop^.a10 :
= Byte(StrToInt(Node1.Attributes['标识'])) xor $80;
      
end;
      myStream.Write(Pgshop^, SizeOf(gshop_info));
    
end;
    myStream.Write(LastRecord[
0], Length(LastRecord));
    myStream.Write(Bottom[
0], Length(Bottom));
    myStream.SaveToFile(
'.\GoodsSale_Tw1.dat');
    myStream.Free;
  
end
end;
原文地址:https://www.cnblogs.com/jxgxy/p/1520447.html