1

I want to creat this xml file from a txt file...

i do this Code:

 FXml := TNativeXml.CreateName('Root');
 FXml.XmlFormat := xfReadable;
 open the file
 AssignFile(TFile,'user.txt');
 Reset(TFile);
 while not eof(TFile) do 
 begin
    Readln(TFile,text);
    r :=  Pos(' ',text);
    t2 := Trim(Copy(text,1,Length(text)));
    t1 := Trim(Copy(t2,0,r));
    FXml.Root.NodeNew('row');
    FXml.Root.NodeByName('row').WriteAttributeString('user',t2);
    FXml.Root.NodeByName('row').WriteAttributeString('pin',t1);
 end;
   FXml.SaveToFile('new.xml');
 FXml.free;

something i do wrong with nodebyname but what...

Thank you...

azrael11
  • 417
  • 6
  • 18
  • You're reading a line from the text file into a variable called 'text', but then you check for the first blank in a non-initialised variable called 't2'. It looks like the 'r:= pos' and 't2:= trim' lines are in the wrong order. – No'am Newman Oct 04 '12 at 07:27
  • Also: Trim(Copy(t2,0,r)); The 0 should be a 1 for delphi strings. – Despatcher Oct 04 '12 at 08:53
  • Sorry about that i correct this ... the lines now is in correct order.. – azrael11 Oct 04 '12 at 13:37

1 Answers1

1

In case your text file contains more than one line, you are creating multiple nodes with the name "row". NodeByName will always return the first node with the given name.

You should store the result of NodeNew in a local variable of type TXmlNode and use that one to set the attributes.

var
  node: TXmlNode
...
node := FXml.Root.NodeNew('row');
node.WriteAttributeString('user',t2);
node.WriteAttributeString('pin',t1);
Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130