Register for your free account! | Forgot your password?

You last visited: Today at 15:19

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Ini to Xml - Converter

Discussion on Ini to Xml - Converter within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Ini to Xml - Converter

This is a simple code to to convert inifiles to xml.
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();
        }
    }
}
Enjoy .
I don't have a username is offline  
Thanks
3 Users
Old 02/27/2012, 19:32   #2
 
JobvdH's Avatar
 
elite*gold: 0
Join Date: Nov 2010
Posts: 371
Received Thanks: 120
I like it, great work!
JobvdH is offline  
Old 02/27/2012, 21:19   #3
 
PretendTime's Avatar
 
elite*gold: 0
Join Date: Nov 2011
Posts: 68
Received Thanks: 9
Oh cool =D thanks
PretendTime is offline  
Old 02/27/2012, 21:43   #4
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,282
Received Thanks: 4,191
You forgot about comments. =p
Spirited is offline  
Old 02/27/2012, 22:54   #5
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by Fаng View Post
You forgot about comments. =p
I also forgot about other things, just a simple one tho.
I don't have a username is offline  
Reply


Similar Threads Similar Threads
[Converter] Free Music Converter
07/19/2010 - Music - 1 Replies
Moin, ich hab für euch mal einen kleinen Musik Converter gemacht! Mit dem Converter könnt ihr die standart files convetieren, wie *.mp3 , *.wav , *.wma und noch mehr! hier ein pic: http://i30.tinypic.com/dy9mqe.png
Converter
04/08/2010 - General Coding - 30 Replies
Hallo Habe eine Frage. Kennt jemand einen Programm mit dem ich eine Javaprogrammierung ausführen kann?? Muss eine Facharbeit für Informatik schreiben. Ich will eine Vergleichsanalyse von C++ und Java anfertigen. Dafür brauche ich in beiden Sprachen daselbe Programm. In C++ habe ich alles. Brauche jetzt nurnoch ein Programm mit dme ich die Javaprogrammierung ausführen kann.
DDS Converter
03/25/2010 - Technical Support - 2 Replies
hi ich suche eine Converter der eine DDS datei in eine tga datein converntiert und wieder zurück^^ Mfg
Looking for SPR converter
11/11/2008 - RF Online - 5 Replies
I've seen posts around and about regarding SPR2DSS. I however can't find any damn link or place to get the thing. If anyone could enlighten me, I'd appreciate it. Thanks.



All times are GMT +1. The time now is 15:19.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.