xml| Conversion
For the development of C/s system programmers, INI file is not unfamiliar, when the XML becomes more and more popular, how to convert the original INI file to XML becomes a tricky thing. Below I put this aspect of the code dedicated to everyone, if there are any bugs, welcome criticism: qieyj@cape.com.cn.
FrmMain.cs
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Namespace Loki
{
<summary>
Summary description for Form1.
</summary>
public class FrmMain:System.Windows.Forms.Form
{
Public System.Windows.Forms.Button Btnconvert;
Public System.Windows.Forms.TextBox Txtxmlfilename;
Public System.Windows.Forms.Label Label2;
Public System.Windows.Forms.TextBox Txtinifilename;
Public System.Windows.Forms.Button btnclose;
Public System.Windows.Forms.Label Label1;
<summary>
Required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Public Frmmain ()
{
//
Required for Windows Form Designer support
//
InitializeComponent ();
}
<summary>
Clean up any being used.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
#region Windows Form Designer generated code
<summary>
///Required to Designer support-do not modify
///The contents of this method with the Code editor.
///</summary>
private void InitializeComponent ()
{
This.btnconvert = new System.Windows.Forms.Button ();
this.txtxmlfilename = new System.Windows.Forms.TextBox ();
this.txtinifilename = new System.Windows.Forms.TextBox ();
this. Label1 = new System.Windows.Forms.Label ();
this.btnclose = new System.Windows.Forms.Button ();
this. Label2 = new System.Windows.Forms.Label ();
this. SuspendLayout ();
//
//Btnconvert
//
This.btnConvert.BackColor = System.Drawing.SystemColors.Control;
this.btnConvert.Cursor = System.Windows.Forms.Cursors.Default;
this.btnConvert.Location = new System.Drawing.Point (160, 96);
this.btnConvert.Name = "Btnconvert";
this.btnConvert.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.btnConvert.Size = new System.Drawing.Size (105, 25);
This.btnConvert.TabIndex = 4;
this.btnConvert.Text = "&convert";
This.btnConvert.Click + = new System.EventHandler (This.btnconvert_click);
//
//Txtxmlfilename
//
This.txtXMLFileName.AcceptsReturn = true;
this.txtXMLFileName.AutoSize = false;
This.txtXMLFileName.BackColor = System.Drawing.SystemColors.Window;
this.txtXMLFileName.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtXMLFileName.ForeColor = System.Drawing.SystemColors.WindowText;
this.txtXMLFileName.Location = new System.Drawing.Point (104, 48);
this.txtXMLFileName.MaxLength = 0;
this.txtXMLFileName.Name = "Txtxmlfilename";
this.txtXMLFileName.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.txtXMLFileName.Size = new System.Drawing.Size (273, 25);
This.txtXMLFileName.TabIndex = 3;
this.txtXMLFileName.Text = "";
//
//Txtinifilename
//
This.txtIniFileName.AcceptsReturn = true;
this.txtIniFileName.AutoSize = false;
this.txtIniFileName.BackColor = System.Drawing.SystemColors.Window;
this.txtIniFileName.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtIniFileName.ForeColor = System.Drawing.SystemColors.WindowText;
this.txtIniFileName.Location = new System.Drawing.Point (104, 16);
this.txtIniFileName.MaxLength = 0;
this.txtIniFileName.Name = "Txtinifilename";
this.txtIniFileName.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.txtIniFileName.Size = new System.Drawing.Size (273, 25);
This.txtIniFileName.TabIndex = 1;
this.txtIniFileName.Text = "E:\\source\\loki\\ini2xml\\vb.net\\projini2xml.net\\sample.ini";
//
Label1
//
this. Label1.autosize = true;
this. Label1.backcolor = System.Drawing.SystemColors.Control;
this. Label1.cursor = System.Windows.Forms.Cursors.Default;
this. Label1.forecolor = System.Drawing.SystemColors.ControlText;
this. Label1.location = new System.Drawing.Point (16, 24);
this. Label1.name = "Label1";
this. Label1.righttoleft = System.Windows.Forms.RightToLeft.No;
this. Label1.size = new System.Drawing.Size (68, 13);
this. Label1.tabindex = 0;
this. Label1.Text = "INI Filename";
//
//Btnclose
//
This.btnClose.BackColor = System.Drawing.SystemColors.Control;
this.btnClose.Cursor = System.Windows.Forms.Cursors.Default;
This.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnClose.Location = new System.Drawing.Point (272, 96);
this.btnClose.Name = "Btnclose";
this.btnClose.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.btnClose.Size = new System.Drawing.Size (105, 25);
This.btnClose.TabIndex = 5;
This.btnClose.Text = "Close";
This.btnClose.Click + = new System.EventHandler (This.btnclose_click);
//
//Label2
//
this. Label2.autosize = true;
This. Label2.backcolor = System.Drawing.SystemColors.Control;
this. Label2.cursor = System.Windows.Forms.Cursors.Default;
this. Label2.forecolor = System.Drawing.SystemColors.ControlText;
this. Label2.location = new System.Drawing.Point (16, 56);
this. Label2.name = "Label2";
this. Label2.righttoleft = System.Windows.Forms.RightToLeft.No;
this. Label2.size = new System.Drawing.Size (77, 13);
this. Label2.tabindex = 2;
this. Label2.Text = "XML Filename";
//
//Frmmain
//
this. AcceptButton = This.btnconvert;
this. AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this. CancelButton = This.btnclose;
this. ClientSize = new System.Drawing.Size (392, 141);
this. Controls.AddRange (new system.windows.forms.control[] {
This.btnconvert,
This.txtxmlfilename,
this. Label2,
This.txtinifilename,
This.btnclose,
This. Label1});
this. Name = "Frmmain";
this. Text = "INI 2 XML";
this. Load + = new System.EventHandler (this.frmmain_load);
this. ResumeLayout (FALSE);
}
#endregion
<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Frmmain ());
}
private void Frmmain_load (object sender, System.EventArgs e)
{
}
private void Btnconvert_click (object sender, System.EventArgs e)
{
if (ini2xml. Convert (Txtinifilename.text, Txtxmlfilename.text))
System.Windows.Forms.MessageBox.Show ("successfully converted \" "+ Txtinifilename.text +" \ "to \" "+ Txtxmlfilename.tex T + "\", this. Text, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
Else
System.Windows.Forms.MessageBox.Show ("Problem converting \" "+ Txtinifilename.text +" \ "to" "+ Txtxmlfilename.text +" \ "", this. Text, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
}
private void btnClose_Click (object sender, System.EventArgs e)
{
This. Close ();
}
}
}
INI2XML.cs
Using System;
Namespace Loki
{
Using System.Runtime.InteropServices;
<summary>
WIN32 API Wrapper class
</summary>
public class Win32wrapper
{
<summary>
Get all of the section names from a INI file
</summary>
[DllImport ("Kernel32.dll", entrypoint= "Getprivateprofilesectionnamesa")]
public extern static int Getprivateprofilesectionnames (
[MarshalAs (UnmanagedType.LPArray)] byte[] lpreturnedstring,
int nsize,
String lpfilename);
<summary>
Get all of the settings from a section in a INI file
</summary>
[DllImport ("Kernel32.dll", entrypoint= "Getprivateprofilesectiona")]
public extern static int getprivateprofilesection (
String Lpappname,
[MarshalAs (UnmanagedType.LPArray)] byte[] lpreturnedstring,
int nsize,
String lpfilename);
}
<summary>
Convert an INI file to an XML file
</summary>
public class Ini2xml
{
<summary>
///
</summary>
Public Ini2xml ()
{
}
<summary>
Initial size of the buffer used when calling the Win32 API functions
</summary>
const int initial_buffer_size = 1024;
///<summary>
///Converts an INI file to an XML file.
///Output XML file has the following structure ...
/// <?xml version= "1.0"
/// <configuration>
/// <section name= "Main"
/// <setting name= "Timeout" value= "/>";
/// <setting name= "Mode" value= "Live"/>
/// </section>
/// </configuration>
///Example:
/// if (Loki.INI2XML.Convert (txtinifilename.text))
/// System.Console.WriteLine ("successfully Converted \ "" + Txtinifilename.text + "\ to xml");
///Else
/// System.Console.WriteLine ("Problem converting \" "+ Txtinifilename.text +" \ "x ML ");
&NBSp If A exception is raised, it's passed on to the caller.
///</summary>
///<param name= "Strinifilename" >file name of the INI File to convert</ Param>
///<returns>true if successfuly, or False if a problem</returns>
public static BOOL Convert (string strinifilename)
{
return Convert (Strinifilename, "");
}
<summary>
///Converts an INI file to an XML file.
///Output XML file has the following structure ...
///<?xml version= "1.0"?>
///<configuration>
///<section name= "Main" >
///<setting name= Timeout value= "/>"
///<setting name= "Mode" value= "Live"/>
///</section>
///</configuration>
///Example:
///if (Loki.INI2XML.Convert (Txtinifilename.text, txtxmlfilename.text))
System.Console.WriteLine ("successfully converted \" "+ Txtinifilename.text +" \ "to \" "+ Txtxmlfilename.text +" \ ") );
///Else
///System.Console.WriteLine ("Problem converting \" "+ Txtinifilename.text +" \ "to" "+ Txtxmlfilename.text +" \"" );
///If A exception is raised, it is passed in to the caller.
///</summary>
///<param name= "Strinifilename" >file name of the INI File to convert</param>
///<param name= "Strxmlfilename" >file name of the XML File this is created</param>
///<returns>true if successfuly, or False if a problem</returns>
public static bool Convert (string strinifilename, string strxmlfilename)
{
char[] Charequals = {' = '};
string lpsections;
int nsize;
int nmaxsize;
string strsection;
int intsection;
int intnamevalue;
string StrName;
string strvalue;
string Strnamevalue;
string lpnamevalues;
int intpos;
byte[] str = new BYTE[1];
System.IO.StreamWriter FileStream;
if (strxmlfilename.length = 0)
{
//Set the XML ' s file name based on the INI file name
intpos = Strinifilename.lastindexof (". ini");
if (intpos >= 0)
{
Strxmlfilename = strinifilename.substring (0, Intpos) + ". xml";
}
Else
{
strxmlfilename = Strinifilename + ". xml";
}
}
//Get all sections names
//Making sure allocate enough spaces for data returned
for (nmaxsize = INITIAL_BUFFER_SIZE/2,
nsize = nmaxsize;
nsize!= 0 && nsize >= (nMaxSize-2);
nmaxsize *= 2)
{
str = new Byte[nmaxsize];
nsize = win32wrapper.getprivateprofilesectionnames (str, nmaxsize, strinifilename);
}
Convert the byte array into a. NET string
lpsections = System.Text.Encoding.ASCII.GetString (str);
Create XML File
FileStream = System.IO.File.CreateText (strxmlfilename);
//Write The opening XML
filestream.writeline ("<?xml version=\ 1.0\"?>< Configuration> ");
//Loop through each section
char[] charnull = {' All '};
for (intsection = 0,
strsection = GetToken (lpsections, Charnull, intsection); br> strsection.length > 0;
strsection = GetToken (lpsections, Charnull, ++intsection))
{
//Write a Node for the section
filestream.writeline ("& Lt;section name=\ "" + strsection + ">");
//Get all values in this section, making sure to allocate enough spaces
& nbsp; for (nmaxsize = initial_buffer_size,
nsize = nmaxsize;
nsize!= 0 && nsize >= (nMaxSize-2);
Nmaxsize *= 2)
{
str = new Byte[nmaxsize];
nsize = win32wrapper.getprivateprofilesection (strsection, str, nmaxsize, strinifilename);
}
//Convert the byte array into a. NET string
lpnamevalues = System.Text.Encodi Ng. Ascii. GetString (str);
//Loop through Name/value pair
for (intnamevalue = 0,
strnamevalue = GetToken (Lpnamevalues, Charnull, Intnamevalue);
strnamevalue.length > 0;
strnamevalue = GetToken (Lpnamevalues, Charnull, ++intnamevalue))
{
//Get the name and value from the entire null separated string of NA Me/value pairs
//Also escape out the special characters, (ie. & "<>)
&nbs p; strName = Xmlencode (GetToken (strnamevalue, charequals, 0));
strvalue = Xmlencode (strnamevalue.substring (strname.length + 1));
//Write XML Name/value Node to the XML file
filestream.write Line ("<setting name=\" "+ strName +" \ "value=\" "+ strvalue +" \ "/>");
}
//Close the section node
filestream.writeline (" </section> ");
}
//thats it
filestream.writeline ("</configuration>");
Filestream.close ();
return true;
}//Convert
///<summary>
///encodes special characters that XML has problems With, ie.
/// character Encoded to
/// &
/// "
/// <
/// > > intsection = 0
/// strsection = GetToken ( Lpsections, Charnull, intsection)
///</summary>
///<param name= "StrText" >text that Needs Encoding</param>
///<returns>encoded text</returns>
public static string Xmlencode (String strText)
{
string strtextret = StrText;
Strtextret = Strtextret.replace ("&", "&");
Strtextret = strtextret.replace ("\", "" ");
Strtextret = Strtextret.replace ("<", "<");
Strtextret = Strtextret.replace (">", ">");
return strtextret;
}
<summary>
Get a token from a delimited string, eg.
intsection = 0
Strsection = GetToken (lpsections, Charnull, intsection)
</summary>
<param name= "StrText" >text This is delimited</param>
<param name= "delimiter" >the delimiter, eg. "," </param>
<param name= "Intindex" >the index of the token to return, NB. The token is index 0.</param>
<returns>returns the nth token from a string.</returns>
private static string GetToken (String strText, char[] delimiter, int intindex)
{
String strtokenret = "";
string[] Strtokens = Strtext.split (delimiter);
if (strtokens.getupperbound (0) >= Intindex)
Strtokenret = Strtokens[intindex];
return strtokenret;
}//GetToken
}//Class Ini2xml
}//Namespace Loki