如何把ini文件转换为xml

对于开发C/S系统的编程人员来说,ini文件是不会陌生的,当XML越来越普及的时候,如何把原来的INI文件转换程XML就成为一件棘手的事情。下面我把这方面的代码奉献给大家,如果有什么bug,欢迎批评指正: [email protected]

frmMain.cs

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Loki
{
///

1<summary>   
2/// Summary description for Form1.   
3/// </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;

///

1<summary>   
2/// Required designer variable.   
3/// </summary>

private System.ComponentModel.Container components = null;

public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}

///

1<summary>   
2/// Clean up any resources being used.   
3/// </summary>

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

1<summary>   
2/// Required method for Designer support - do not modify   
3/// the contents of this method with the code editor.   
4/// </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

///

1<summary>   
2/// The main entry point for the application.   
3/// </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.Text + """, 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;

///

1<summary>   
2/// WIN32 API Wrapper class   
3/// </summary>

public class WIN32Wrapper
{
///

1<summary>   
2/// Get all the section names from an INI file   
3/// </summary>

[ DllImport("kernel32.dll", EntryPoint="GetPrivateProfileSectionNamesA") ]
public extern static int GetPrivateProfileSectionNames(
[MarshalAs(UnmanagedType.LPArray)] byte[] lpReturnedString,
int nSize,
string lpFileName);

///

1<summary>   
2/// Get all the settings from a section in a INI file   
3/// </summary>

[ DllImport("kernel32.dll", EntryPoint="GetPrivateProfileSectionA") ]
public extern static int GetPrivateProfileSection(
string lpAppName,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpReturnedString,
int nSize,
string lpFileName);
}

///

1<summary>   
2/// Convert an INI file into an XML file   
3/// </summary>

public class INI2XML
{
///

1<summary>   
2///   
3/// </summary>

public INI2XML()
{
}

///

1<summary>   
2/// Initial size of the buffer used when calling the Win32 API functions   
3/// </summary>

const int INITIAL_BUFFER_SIZE = 1024;

///

 1<summary>   
 2/// Converts an INI file into an XML file.   
 3/// Output XML file has the following structure...   
 4/// <?xml version="1.0"?>   
 5/// <configuration>   
 6/// <section name="Main">   
 7/// <setting name="Timeout" value="90"></setting>   
 8/// <setting name="Mode" value="Live"></setting>   
 9/// </section>   
10/// </configuration>   
11/// Example:   
12/// if (Loki.INI2XML.Convert( txtIniFileName.Text ))   
13/// System.Console.WriteLine( "Successfully converted \"" + txtIniFileName.Text + "\" to xml" );   
14/// else   
15/// System.Console.WriteLine( "Problem converting \"" + txtIniFileName.Text + "\" to xml" );   
16/// If an exception is raised, it is passed on to the caller.   
17/// </summary>

///

1<param name="strINIFileName"/>

File name of the INI file to convert
///

1<returns>True if successfuly, or False if a problem</returns>

public static bool Convert( string strINIFileName )
{
return Convert( strINIFileName, "" );
}

///

 1<summary>   
 2/// Converts an INI file into an XML file.   
 3/// Output XML file has the following structure...   
 4/// <?xml version="1.0"?>   
 5/// <configuration>   
 6/// <section name="Main">   
 7/// <setting name="Timeout" value="90"></setting>   
 8/// <setting name="Mode" value="Live"></setting>   
 9/// </section>   
10/// </configuration>   
11/// Example:   
12/// if (Loki.INI2XML.Convert( txtIniFileName.Text, txtXMLFileName.Text ))   
13/// System.Console.WriteLine( "Successfully converted \"" + txtIniFileName.Text + "\" to \"" + txtXMLFileName.Text + "\"" );   
14/// else   
15/// System.Console.WriteLine( "Problem converting \"" + txtIniFileName.Text + "\" to \"" + txtXMLFileName.Text + "\"" );   
16/// If an exception is raised, it is passed on to the caller.   
17/// </summary>

///

1<param name="strINIFileName"/>

File name of the INI file to convert
///

1<param name="strXMLFileName"/>

File name of the XML file that is created
///

1<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 space 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( "

 1<configuration>" );   
 2  
 3// Loop through each section   
 4char[] charNull = {'\0'};   
 5for (intSection = 0,   
 6strSection = GetToken(lpSections, charNull, intSection);   
 7strSection.Length &gt; 0;   
 8strSection = GetToken(lpSections, charNull, ++intSection) )   
 9{   
10// Write a Node for the Section   
11fileStream.WriteLine( "<section "\"="" +="" name='\""' strsection="">" );   
12  
13// Get all values in this section, making sure to allocate enough space   
14for (nMaxSize = INITIAL_BUFFER_SIZE,   
15nSize = nMaxSize;   
16nSize != 0 &amp;&amp; nSize &gt;= (nMaxSize-2);   
17nMaxSize *= 2)   
18{   
19str = new Byte[nMaxSize];   
20nSize = WIN32Wrapper.GetPrivateProfileSection(strSection, str, nMaxSize, strINIFileName);   
21} 
22
23// convert the byte array into a .NET string   
24lpNameValues = System.Text.Encoding.ASCII.GetString( str );   
25  
26// Loop through each Name/Value pair   
27for (intNameValue = 0,   
28strNameValue = GetToken(lpNameValues, charNull, intNameValue);   
29strNameValue.Length &gt; 0;   
30strNameValue = GetToken(lpNameValues, charNull, ++intNameValue) )   
31{   
32// Get the name and value from the entire null separated string of name/value pairs   
33// Also escape out the special characters, (ie. &amp;"&lt;&gt; )   
34strName = XMLEncode( GetToken( strNameValue, charEquals, 0 ) );   
35strValue = XMLEncode( strNameValue.Substring( strName.Length + 1 ) ); 
36
37// Write the XML Name/Value Node to the xml file   
38fileStream.WriteLine( "<setting "\"="" +="" name='\""' strname="" strvalue="" value='\""'></setting>");   
39}   
40  
41// Close the section node   
42fileStream.WriteLine( "</section>" );   
43}   
44  
45// Thats it   
46fileStream.WriteLine( "</configuration>

" );
fileStream.Close();

return true;
} // Convert

///

1<summary>   
2/// Encodes special characters that XML has problems with, ie.   
3/// Character Encoded to   
4/// &amp; &amp;   
5/// " "   
6/// &lt; &lt;   
7/// &gt; &gt; intSection = 0   
8/// strSection = GetToken(lpSections, charNull, intSection)   
9/// </summary>

///

1<param name="strText"/>

Text that needs encoding
///

1<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;
}

///

1<summary>   
2/// Get a token from a delimited string, eg.   
3/// intSection = 0   
4/// strSection = GetToken(lpSections, charNull, intSection)   
5/// </summary>

///

1<param name="strText"/>

Text that is delimited
///

1<param name="delimiter"/>

The delimiter, eg. ","
///

1<param name="intIndex"/>

The index of the token to return, NB. first token is index 0.
///

1<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

Published At
Categories with Web编程
Tagged with
comments powered by Disqus