Question : XML Read issue


I have the following XML file:


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
  <File>
    <Number>1001</Number>
    <Name>Joe Bloggs</Name>
    <Town>Washingon</Town>
    <Payment>
      <Date>20100101</Date>
      <Amount>5000</Amount>
    </Payment>
    <Payment>
      <Date>20100102</Date>
      <Amount>10000</Amount>
    </Payment>
  </File>
  <File>
    <Number>1002</Number>
    <Name>John Smith</Name>
    <Town>Miami</Town>
    <Payment>
      <Date>20101101</Date>
      <Amount>21000</Amount>
    </Payment>
  </File>
</Employees>


I have following code which is able to read the nodes correctly except for the last child node i.e. payment.date. Can someone tell me where the issue is with my code in trying to retrieve the Date value in the Payment Node?

Many thanks.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
var
  iNumber, iName, iTown, iDate: string;
  Employees, Payments: IXMLNode;
  i: integer;
begin

   XMLDocument1.Active := False;
  XMLDocument1.XML.LoadFromFile(
    'C:\Temp4-XML\Project1\XMLSourceFile\SourceFile - MasterDetail.xml');
  XMLDocument1.Active := True;

   Employees := XMLDocument1.DocumentElement;

  for i := 0 to Employees.ChildNodes.Count - 1 do
  begin

    iNumber := Employees.ChildNodes[i].ChildNodes['Number'].Text;
    iName := Employees.ChildNodes[i].ChildNodes['Name'].Text;
    iTown := Employees.ChildNodes[i].ChildNodes['Town'].Text;

   Payments := Employees.ChildNodes[i].ChildNodes['Payments'];
    iDate := Payments.ChildNodes['Date'].Text;

    ListBox1.Items.Add(iNumber + ',' + iName + ',' + iTown + ',' + iDate);
  end;

end;

Answer : XML Read issue

Payments := Employees.ChildNodes[i].ChildNodes['Payments'];
should be
Payments := Employees.ChildNodes[i].ChildNodes['Payment'];

Your XML tag is Payment, not Payments.
Random Solutions  
 
programming4us programming4us