Quantcast

Convert INI file to XML

Get the WebProNews Newsletter:
[ Business]

What is it?

Do you think XML is a better way of storing an application’s configuration than old fashioned INI files? Of course it is. So you probably need to convert your INI files into XML. Read on for a solution to just this task.

I started writing the app in VB 6, but when I finished I thought, why not do it in VB.NET and C# as well, so the code is provided in these three languages.

The .NET versions use the .NET Frameworks XmlTextWriter class to write the Xml file. This class is very cool, as it will handle escaping any special characters for you (like &, < and >), and just makes writing Xml easy.

How do I use it?

1. Add a reference to the INI2XML assembly.

2. Add using Loki or Imports Loki add the top of your code, as it’s defined in the Loki namespace.

3. Then call the function Convert in the INI2XML class. As the Convert function is static/Shared, you don’t need to create an instance of the class.

Examples

C#

INI2XML.Convert( "C:WINNTiexplore.ini" );

VB.NET

INI2XML.Convert( "C:WINNTiexplore.ini" )

VB 6

Call INI2XML( "C:WINNTiexplore.ini", "" )

It will create an xml file of the same name, with a .xml extension unless you pass the xml file name as the second parameter.

How does it do it?

The reading of the data from the INI files is done by two nifty WIN32 API functions. GetPrivateProfileSectionNames gets the names of all the sections in the INI file, and GetPrivateProfileSection gets all the settings for an individual section.

Using this approach meant I didn’t have to write much parsing code to interpret the INI file format.

To write the XML file I decided to use the simplest method available. In VB 6 I used good old Open, Print and Close. In VB.Net and C#, I used the .NET Framework’s System.Xml.XmlTextWriter

Basically the code goes something like this:

1. Write an opening root XML tag

2. Get all the section names using GetPrivateProfileSectionNames

3. For each section

&nbsp&nbsp a. Write an opening XML node for the section

&nbsp&nbsp b. Get all the settings in the section using GetPrivateProfileSection

&nbsp&nbsp c. For each setting

&nbsp&nbsp&nbsp&nbsp i. Write an XML node for the setting

&nbsp&nbsp d. Write a closing XML tag for the section

4. Write a closing root XML tag

Reading Values from the Converted XML

Some people have asked me how to use the xml file once its created. So heres an example of reading two values from the Xml file. It gets the Home Page and Search Page for Internet Explorer.

It basically does the following:

1. loads the xml file into a XmlDocument

2. uses XPath to get the appropriate node

3. gets the value from the node Attributes collection

C# Example

private string GetConfigValue( XmlDocument xmlDoc, string sectionName, 
    string settingName )
{
  string valueRet;

  // get setting node
  string xpath = 
    String.Format( "//section[@name='{0}']/setting[@name='{1}']", 
    sectionName, settingName );
  XmlNode node = xmlDoc.DocumentElement.SelectSingleNode( xpath );

  // display value
  if ( node == null )
  {
    throw new Exception( 
      String.Format( 
        "No such setting, using the following xpath:{0}{1}", 
           Environment.NewLine, xpath ) );
  }
  else
  {
    XmlAttribute xmlAttr = node.Attributes["value"];
    if ( xmlAttr == null )
    {
      throw new Exception( String.Format( 
          "No value for this setting, using the following xpath:{0}{1}", 
          Environment.NewLine, xpath ) );
    }
    else
    {
      valueRet = xmlAttr.Value;
    }
  }

  return valueRet;
}

// Load configuration from xml file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load( "C:WINNTiexplore.xml" );
string homePage = GetConfigValue( xmlDoc, "main", "Home Page" );
string searchPage = GetConfigValue( xmlDoc, "main", "Search Page" );

Finally

Feel free to use this code in your projects, and modify it for your specific requirements. For example, you may want to put the VB 6 code into an ActiveX DLL, or you may want to copy the comments from the source INI file into the destination XML file.

I have been in the software industry since the mid 80′s.
In 2002 I founded a software development company (Loki Limited).
We focus on using Microsoft .Net technologies to deliver business solutions
to our customers. Our motto is “It just works”, (ie. robust and secure).
Loki has recently formed a strategic alliance with Majic Technologies and
Max-Gen Media. Visit us at http://www.loki.co.nz or the Cube Alliance at
http://www.cubealliance.com

Convert INI file to XML
Comments Off
About Grant Phillips
I have been in the software industry since the mid 80's. In 2002 I founded a software development company (Loki Limited). We focus on using Microsoft .Net technologies to deliver business solutions to our customers. Our motto is "It just works", (ie. robust and secure). Loki has recently formed a strategic alliance with Majic Technologies and Max-Gen Media. Visit us at http://www.loki.co.nz or the Cube Alliance at http://www.cubealliance.com WebProNews Writer
Top Rated White Papers and Resources

Comments are closed.