Thursday, March 5, 2015

Reading an INI file with C#

I had a similar requirement to one that Ajit Porlekar mentioned, converting INI content to XML and here is my solution in case anyone looks for this:



var iniFile = new IniFile();
iniFile.Load("Employees.ini");

var xmlFile = new XDocument(
new XElement("sections",
from iniSection in iniFile.Sections
select
new XElement("section",
new XAttribute("name", iniSection.Name),
from iniKey in iniSection.Keys
select
new XElement("item",
new XAttribute("key", iniKey.Name),
new XAttribute("value", iniKey.Value)))));
xmlFile.Save("Employees.xml");

Note that I used XDocument for creating an XML file and this library for reading INI files in C#.


No comments:

Post a Comment