How to Turn an INI File into XML
XML has become the de facto standard for storing configuration data in modern applications. Unlike the flat, section‑based INI files that were popular in the early days of Windows programming, XML allows you to represent hierarchical data, attach metadata, and apply schema validation. If you’re maintaining legacy software that still relies on INI files, or you simply want to migrate to a more structured format, converting those files to XML is a logical first step. The approach outlined below works across VB 6, VB.NET, and C#, giving you a single reusable library that can be dropped into any project.
The library, named INI2XML, is deliberately lightweight. It bundles two Windows API calls – GetPrivateProfileSectionNames and GetPrivateProfileSection – to pull sections and key/value pairs from an INI file without having to parse the file manually. For the XML output, the .NET implementations leverage System.Xml.XmlTextWriter, which automatically handles character escaping and formatting. The VB 6 version uses the classic file I/O functions (Open, Print, Close) because that environment doesn’t have the richer XML classes available.
To get started, add a reference to the INI2XML.dll that ships with the library. In a .NET project you then add using Loki; at the top of your file (or Imports Loki in VB.NET). The library exposes a single static method, Convert, which accepts the path to an INI file and an optional path for the resulting XML file. Because the method is static, there’s no need to instantiate INI2XML; just call it directly:
or in VB.NET:
In VB 6 the function is a standard module procedure and requires a second argument for the output file name (if you leave it blank, the library will create an XML file with the same base name as the INI). The resulting XML file will have a root element named configuration and a section element for each INI section. Within each section there are setting elements, each carrying a name attribute for the key and a value attribute for the corresponding value. An example output looks like this:
The conversion routine works by first writing the opening <configuration> tag, then iterating over every section name returned by GetPrivateProfileSectionNames. For each section, it opens a new <section> element and writes each key/value pair inside <setting> tags. Once all sections have been processed, it writes the closing </configuration> tag and flushes the writer. In .NET the XmlTextWriter automatically inserts line breaks and indentation for readability, whereas the VB 6 version uses simple text printing.
Once you have the XML file, you can load it into any XML‑aware component. A typical use case is to read specific configuration values at runtime. Below is a C# helper that demonstrates how to pull the values of two settings from the XML. The function accepts an XmlDocument, the name of the section, and the name of the setting, and returns the value as a string. It builds an XPath expression that points to the relevant setting node and extracts the value attribute. If the node or attribute is missing, it throws an informative exception.
Using the helper is straightforward. Load the XML document once, then query individual values as needed:
Because the library is written in plain C# and VB.NET, you can easily adapt or extend it. If you want the INI parser to preserve comments, you could modify the Windows API calls to include the comments as attributes or child nodes. If you prefer a different XML schema – for example, using nested elements instead of attributes – you can swap out the XmlTextWriter logic for your own serialization code. The VB 6 version is similarly modular; it simply writes text lines, so replacing it with an ActiveX DLL or COM component is trivial.
To make the conversion process painless in a larger project, consider adding the INI2XML project to your solution and referencing it from each module that needs configuration data. If you’re working on a cross‑platform application, you can keep the VB 6 portion as a legacy component while writing new services in .NET that read the XML directly. Because the output format is machine‑readable, you can even feed the same XML file into third‑party tools that require configuration in XML.
In sum, converting INI files to XML is a small investment that unlocks powerful benefits: structured data, easier validation, and better integration with modern frameworks. By following the steps above, you can add a ready‑made converter to your toolbox, write concise code that reads the resulting XML, and evolve your application without the constraints of legacy INI parsing.





No comments yet. Be the first to comment!