Author: Jim McKeeth
INI was the old way to store settings (outside the registry), now everything is
XML. This routine will convert an INI file into an XML node of a document.
Answer:
An XML node is part of an XML Document. So if you have a new XMLDocument then just
add a single node (AddChild) and that is the DocumentNode, then you could pass the
DocumentNode to this routine. If you pass in different Nodes then you could store
multiple INI files in the same XML document, even if they have section and value
name collisions. The AsAttributes parameter determines if the values are stored as
Attributes (default) or sub-nodes.
1 uses XMLIntf, XMLDoc, INIFiles;
2
3 procedure INI2XML(const pINIFileName: string; const pXML: IXMLNode;
4 const AsAttributes: Boolean = true);
5 var
6 lINIFile: TIniFile;
7 lSections, lItems: TStringList;
8 iSections, iItems: integer;
9 lNode: IXMLNode;
10 begin
11 lINIFile := TIniFile.Create(pINIFileName);
12 try
13 lSections := TStringList.Create;
14 try
15 lItems := TStringList.Create;
16 try
17
18 lINIFile.ReadSections(lSections);
19
20 for iSections := 0 to pred(lSections.Count) do
21 begin
22 lItems.Clear;
23 lINIFile.ReadSection(lSections[iSections], lItems);
24 lNode := pXML.AddChild(StringReplace(lSections[iSections], ' ', '',
25 [rfReplaceAll]));
26 for iItems := 0 to pred(lItems.Count) do
27 begin
28 if AsAttributes then
29 lNode.Attributes[lItems[iItems]] :=
30 lINIFile.ReadString(lSections[iSections], lItems[iItems], '')
31 else
32 lNode.AddChild(lItems[iItems]).Text :=
33 lINIFile.ReadString(lSections[iSections], lItems[iItems], '');
34 end;
35 lNode := nil;
36 end;
37
38 finally lItems.Free;
39 end;
40 finally lSections.Free;
41 end;
42 finally lINIFile.Free;
43 end;
44 end;
|