Click here to Skip to main content
13,053,276 members (61,359 online)
Click here to Skip to main content
Add your own
alternative version

Stats

256.8K views
2.4K downloads
87 bookmarked
Posted 27 Mar 2002

Convert INI file to XML

, 29 Jan 2007
Rate this:
Please Sign up or sign in to vote.
You probably need to convert your old INI files into XML. Read on for a solution.

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:\\WINNT\\iexplore.ini" );

VB.NET

INI2XML.Convert( "C:\WINNT\iexplore.ini" )

VB 6

Call INI2XML( "C:\WINNT\iexplore.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
    1. Write an opening XML node for the section
    2. Get all the settings in the section using GetPrivateProfileSection
    3. For each setting
      1. Write an XML node for the setting
    4. 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:\\WINNT\\iexplore.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.

History

  • 30 Jan 2007 - updated download

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Loki
Software Developer (Senior) Loki Limited
New Zealand New Zealand
I have been in the software industry since the mid 80's.
Most of my time is in the Microsoft .NET world, C# and SQL. Since 2013 I've been developing iOS apps in my spare time.
about me

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
raddevus24-Feb-17 9:40
mvpraddevus24-Feb-17 9:40 
QuestionGod bless you for the hours you saved me! Pin
Jamie Llewellyn21-Sep-11 7:27
memberJamie Llewellyn21-Sep-11 7:27 
GeneralXML2INI? [modified] Pin
b4bu1-Mar-07 22:00
memberb4bu1-Mar-07 22:00 
Generalini to XmlDoc or XmlElement Pin
Aubyone21-Oct-04 13:33
memberAubyone21-Oct-04 13:33 
GeneralRe: ini to XmlDoc or XmlElement Pin
Loki22-Oct-04 0:40
memberLoki22-Oct-04 0:40 
QuestionBogus Code? Bogus Example? Pin
schor29-Sep-04 19:01
memberschor29-Sep-04 19:01 
AnswerRe: Bogus Code? Bogus Example? Pin
Loki3-Oct-04 11:23
memberLoki3-Oct-04 11:23 
QuestionCan you provide VC++6 source code??? Pin
knight_zhuge14-Mar-04 18:03
memberknight_zhuge14-Mar-04 18:03 
GeneralVery large INI file Pin
PeterJ4-Oct-03 16:18
sussPeterJ4-Oct-03 16:18 
GeneralRe: Very large INI file Pin
Loki5-Oct-03 1:28
memberLoki5-Oct-03 1:28 
GeneralRe: Very large INI file Pin
daluu29-Jan-07 15:26
memberdaluu29-Jan-07 15:26 
AnswerRe: Very large INI file Pin
Loki29-Jan-07 23:34
memberLoki29-Jan-07 23:34 
GeneralRe: Very large INI file Pin
mattcamp17-Apr-12 9:40
membermattcamp17-Apr-12 9:40 
GeneralHandling Comment in INI Pin
PiPadma6-Jun-03 7:37
memberPiPadma6-Jun-03 7:37 
GeneralStreamWriter vs. XmlTextWriter Pin
Heath Stewart28-Apr-03 6:20
editorHeath Stewart28-Apr-03 6:20 
GeneralRe: StreamWriter vs. XmlTextWriter Pin
Loki3-May-03 0:37
memberLoki3-May-03 0:37 
GeneralRe: StreamWriter vs. XmlTextWriter Pin
Heath Stewart3-May-03 1:47
editorHeath Stewart3-May-03 1:47 
QuestionCould I get this compiled into a DLL Pin
mr_java6610-Jan-03 4:46
membermr_java6610-Jan-03 4:46 
AnswerRe: Could I get this compiled into a DLL Pin
Loki10-Jan-03 9:37
memberLoki10-Jan-03 9:37 
QuestionUnicode? Pin
Feng Qin27-Sep-02 3:24
memberFeng Qin27-Sep-02 3:24 
AnswerRe: Unicode? Pin
Loki1-Aug-03 17:43
memberLoki1-Aug-03 17:43 
GeneralI don't think it is a good idea to replace INI file with XML. Pin
Pupil23-May-02 8:09
memberPupil23-May-02 8:09 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Rama Krishna23-May-02 8:17
memberRama Krishna23-May-02 8:17 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Nish - Native CPian23-May-02 8:46
memberNish - Native CPian23-May-02 8:46 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Rama Krishna23-May-02 8:56
memberRama Krishna23-May-02 8:56 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Loki24-May-02 0:47
memberLoki24-May-02 0:47 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
John M. Drescher20-Sep-03 6:19
memberJohn M. Drescher20-Sep-03 6:19 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Loki20-Sep-03 11:48
memberLoki20-Sep-03 11:48 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Nazeeh20-Sep-03 18:18
memberNazeeh20-Sep-03 18:18 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Loki20-Sep-03 18:43
memberLoki20-Sep-03 18:43 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
paillave30-Jan-07 0:06
memberpaillave30-Jan-07 0:06 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
John M. Drescher20-Sep-03 18:25
memberJohn M. Drescher20-Sep-03 18:25 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
abstractsagacity3-Jan-06 18:21
memberabstractsagacity3-Jan-06 18:21 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Vasudevan Deepak Kumar29-Jan-07 22:49
memberVasudevan Deepak Kumar29-Jan-07 22:49 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
dvhh30-Jan-07 2:47
memberdvhh30-Jan-07 2:47 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Carl Lambrecht23-Sep-03 3:20
memberCarl Lambrecht23-Sep-03 3:20 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Anonymous11-Nov-03 19:38
sussAnonymous11-Nov-03 19:38 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
rbid24-Feb-05 3:27
memberrbid24-Feb-05 3:27 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
Loki24-Feb-05 18:24
memberLoki24-Feb-05 18:24 
GeneralRe: I don't think it is a good idea to replace INI file with XML. Pin
paillave30-Jan-07 0:09
memberpaillave30-Jan-07 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 Jan 2007
Article Copyright 2002 by Loki
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid