All you do is specifying the directory where the inifiles are located and it will convert them to xml. The new xml files will be located in same folder.
The code can of course be improved, will come up with a xml wrapper including mysql, mssql and regular wrapping.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IniToXml
{
class Program
{
static void Main(string[] args)
{
Console.Write("Directory: ");
string directory = Console.ReadLine();
Sections = new Dictionary<string, Dictionary<string, string>>();
foreach (string file in System.IO.Directory.GetFiles(directory))
{
if (file.EndsWith(".ini"))
{
File = file.Replace(".ini", ".xml");
CreateXmlFile();
string[] file_read = System.IO.File.ReadAllText(file).Replace("\r", "").Split('\n');
string curSection = "";
foreach (string line in file_read)
{
if (!string.IsNullOrEmpty(line) && !string.IsNullOrWhiteSpace(line))
{
string Line = line;
if (Line.StartsWith("[") && Line.EndsWith("]"))
{
Line = Line.Replace("[", "").Replace("]", "");
curSection = Line;
CreateXmlSection(Line);
}
else if (Line.Contains("="))
{
string[] res = Line.Split('=');
if (res.Length == 2)
{
if (res[1] != null)
{
CreateXmlField(curSection, res[0], res[1]);
}
else
{
CreateXmlField(curSection, res[0], string.Empty);
}
}
else
{
CreateXmlField(curSection, res[0], string.Empty);
}
}
}
}
FinishXmlFile();
}
}
}
static Dictionary<string, Dictionary<string, string>> Sections;
static string File;
static void CreateXmlFile()
{
System.IO.FileStream fs;
if (System.IO.File.Exists(File))
fs = new System.IO.FileStream(File, System.IO.FileMode.Truncate);
else
fs = new System.IO.FileStream(File, System.IO.FileMode.CreateNew);
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs))
{
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sw.WriteLine(string.Empty);
}
fs.Close();
}
static void CreateXmlSection(string section)
{
if (!Sections.ContainsKey(section))
Sections.Add(section, new Dictionary<string, string>());
}
static void CreateXmlField(string section, string fieldname, object value)
{
if (!Sections[section].ContainsKey(fieldname))
Sections[section].Add(fieldname, value == null ? string.Empty : value.ToString());
}
static void FinishXmlFile()
{
using (System.IO.FileStream fs = new System.IO.FileStream(File, System.IO.FileMode.Append))
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs))
{
foreach (string section in Sections.Keys)
{
sw.WriteLine(SetXml(section));
foreach (string field in Sections[section].Keys)
{
sw.WriteLine("\t" + SetXmlValue(field, Sections[section][field]));
}
sw.WriteLine(SetXml(section).Replace("<", "</"));
}
}
}
}
static string SetXml(string name)
{
StringBuilder sb = new StringBuilder();
sb.Append("<").Append(name).Append(">");
return sb.ToString();
}
static string SetXmlValue(string name, string value)
{
StringBuilder sb = new StringBuilder();
sb.Append("<").Append(name).Append(">").Append(value).Append("</").Append(name).Append(">"); ;
return sb.ToString();
}
}
}






