Created
October 18, 2021 22:19
-
-
Save DreamVB/2f300b4a2d83127b1b40fa634366c48d to your computer and use it in GitHub Desktop.
Simple XML Config class for reading and writing basic config files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Forms; | |
using DreamVB.XML.Config; | |
namespace XmlTest | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
XmlConfig cfg = new XmlConfig(); | |
private void button2_Click(object sender, EventArgs e) | |
{ | |
cfg.OpenConfigFile("test.xml", false); | |
//Read from selection name and score | |
MessageBox.Show(cfg.ReadValue("main", "name")); | |
MessageBox.Show(cfg.ReadValue("score", "bob")); | |
//Close xml reader | |
cfg.CloseConfigReader(); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
cfg.OpenConfigFile("test.xml"); | |
//Write main selection | |
cfg.WriteValue("main", "name", "Jan"); | |
cfg.WriteValue("main", "Age", "36"); | |
cfg.WriteValue("main", "gender", "female"); | |
//Next selection score | |
cfg.WriteValue("score", "Bob", "50"); | |
cfg.WriteValue("score", "Jane", "85"); | |
cfg.WriteValue("score", "Jack", "48"); | |
//Close xml writer | |
cfg.CloseConfigWriter(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple XML Config class for reading and writing basic config files. | |
// Version beta 1 | |
// By DreamVB | |
using System; | |
using System.Text; | |
using System.Xml; | |
namespace DreamVB.XML.Config | |
{ | |
public class XmlConfig | |
{ | |
private XmlWriter xml_w = null; | |
private XmlReader xml_r = null; | |
public void OpenConfigFile(string Filename, bool IsWriteing = true) | |
{ | |
//Check if writing to the config file. | |
if (IsWriteing) | |
{ | |
XmlWriterSettings settings = new XmlWriterSettings(); | |
try | |
{ | |
//Used for formatting the config file | |
settings.Indent = true; | |
settings.IndentChars = ("\t"); | |
settings.OmitXmlDeclaration = true; | |
settings.Encoding = Encoding.Unicode; | |
//Create xml file | |
xml_w = XmlWriter.Create(Filename, settings); | |
//Write start of doc | |
xml_w.WriteStartDocument(); | |
//Write main element | |
xml_w.WriteStartElement("config"); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
else | |
{ | |
//User wants to open a file. | |
try | |
{ | |
//Open xml reader. | |
xml_r = new XmlTextReader(Filename); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
} | |
public void CloseConfigWriter() | |
{ | |
try | |
{ | |
xml_w.WriteEndElement(); | |
xml_w.Close(); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
public void CloseConfigReader() | |
{ | |
try | |
{ | |
//Close xml reader | |
xml_r.Close(); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
public void WriteValue(string selection, string key, string value) | |
{ | |
try | |
{ | |
xml_w.WriteStartElement(selection); | |
xml_w.WriteAttributeString(key, value); | |
xml_w.WriteEndElement(); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
public string ReadValue(string selection, string key) | |
{ | |
string sValue = string.Empty; | |
bool CanExit = false; | |
try | |
{ | |
while (xml_r.Read()) | |
{ | |
//Set can exit to false | |
CanExit = false; | |
if (xml_r.NodeType == XmlNodeType.Element) | |
{ | |
//Check for selection name | |
if (xml_r.Name.Equals(selection, StringComparison.OrdinalIgnoreCase)) | |
{ | |
//Check attributes count | |
if (xml_r.AttributeCount > 0) | |
{ | |
//Loop though the attributes | |
for (int y = 0; y < xml_r.AttributeCount; y++) | |
{ | |
//Get next attribute | |
xml_r.MoveToAttribute(y); | |
//Check if key was found in the selection | |
if (xml_r.Name.Equals(key, StringComparison.OrdinalIgnoreCase)) | |
{ | |
//Get value and exit | |
sValue = xml_r.Value; | |
CanExit = true; | |
break; | |
} | |
} | |
} | |
} | |
//Break out of loop | |
if (CanExit) | |
{ | |
break; | |
} | |
} | |
} | |
return sValue; | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment