XML Configuration File for .Net Projects

ConfigurationManager class is used to load settings from an XML file at the start of an application. Usually people edit it outside the application (manually with a text editor), however, it is possible to change configuration inside the program:

http://blog.sb2.fr/post/2008/11/29/HowTo-Modify-Configuration-File-Programmatically-with-C.aspx

The Jabber-Net library uses this function to update its configuration file instead:

/// <summary>

/// Write the current connection properties to an XML config file.

/// TODO: Replace this with a better ConfigFile implementation that can write.

/// </summary>

/// <param name=”file”></param>

public void WriteToFile(string file)

{

XmlDocument doc = new XmlDocument();

string name = “JabberClient”;

if (m_xmpp != null)

name = m_xmpp.GetType().Name;

XmlElement root = (XmlElement)doc.CreateElement(name);

doc.AppendChild(root);

WriteElem(root, this);

foreach (DictionaryEntry ent in m_extra)

{

root.AppendChild(doc.CreateElement((string)ent.Key)).InnerText = ent.Value.ToString();

}

XmlTextWriter xw = new XmlTextWriter(file, System.Text.Encoding.UTF8);

xw.Formatting = Formatting.Indented;

doc.WriteContentTo(xw);

xw.Close();

}

More:

More advanced configuration library: http://www.codeproject.com/KB/cs/SysConfiguration.aspx

You must be logged in to post a comment.