BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


Uncategorized
Fullxml 2
Sorting the results and use the Axis
TreeMaker
XMLEditor - WebClipping
WDDX Parser
Templx: x++ IDE Released!
XppEngCOM: COM implementation of x++ interpreter
Backup Outlook mail as XML
Grouping and Summing the values in XML
WDDX Parser (updated bug)
DIME Parser and Generator (C#)
Flash menu using XML
Virtual Directory Utilities for IIS(C#)
ComSerialization
Re: Max() function
Stop Tailoring, Create Generic!!!
Introduction to WAP Push
Add xml binding to select list
Ontology Web Language
Wrapping multiple elements in a parent element


 
 

<< System.XMLWCF, WS, SOAP >>


By Grant Phillips
First Posted 03/27/2002
Times viewed 533

Converting INI files to XML


This post contains attachments
v20020327055221.zip 

Summary Do you think XML is a better way of storing an application’s configuration that 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.

What is it?

Do you think XML is a better way of storing an application’s configuration that 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.

How do I use it?

Add a reference to the INI2XML assembly.
Add using Loki or Imports Loki add the top of your code. As its defined in the Loki namespace.
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.

C# Example:
INI2XML.Convert( C:\\WINNT\\iexplore.ini );

VB.NET Example:
INI2XML.Convert( C:\WINNT\iexplore.ini )

VB 6 Example:
Call INI2XML( C:\WINNT\iexplore.ini, )
PS. It will create an xml file with 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 simpliest 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.IO.StreamWriter

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

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.

In a future article, I’ll show examples of reading from the XML files to get the applications configuration.


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< System.XMLWCF, WS, SOAP >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML