I try to merge two XML files (created with MSword) with the code bellow but although it runs without any error, i receive an error message when i try to open the merged file. Actually the merged file contains the first's file content plus only the first line of the second (the line " < ?xml version="1.0" encoding="UTF-8" standalone="yes" ? > "). No merged contents!
// button select the first file then button2 selects the second and merges them
uses XML.xmlDoc,XML.XMLintf;
var XMLDoc1, XMLDoc2 : TXMLDocument;
procedure TForm30.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then begin
XMLDoc1 := TXMLDocument.Create(self);
XMLDoc1.Active := True;
XMLDoc1.LoadFromFile(OpenDialog1.filename);
end;
end;
procedure TForm30.Button2Click(Sender: TObject);
var ANode: IXMLNode;
begin
if OpenDialog1.Execute then begin
XMLDoc2 := TXMLDocument.Create(self);
xmlDoc2.Active:= True;
XMLDoc2.LoadFromFile(OpenDialog1.filename);
ANode:= XMLDoc2.ChildNodes[0].CloneNode(True);
xmlDoc1.ChildNodes.add(ANode);
xmlDoc2.Active:= False;
xmlDoc1.SaveToFile('c:\' + 'Merge.xml');
XMLDoc1.free;
XMLDoc2.free;
aNode.free;
end;
end;
What i'm doing wrong, please ? thanks in advance.
XMLDoc2.ChildNodes[0]
may not be the top-level document element, it could be other node types. In this case, it sounds it is the prolog node. You should use theXMLDoc2.DocumentElement
property instead. – Remy Lebeau