Click here to Skip to main content
15,886,664 members
Articles / Programming Languages / XML

Read/Write Configuration in XML file

Rate me:
Please Sign up or sign in to vote.
3.52/5 (20 votes)
9 Sep 20051 min read 88.9K   2.1K   31  
An article on how to read/write App configuration data in an easy way.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace XmlConfig
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtCountry;
		private System.Windows.Forms.Button btnUpdate;
		private Config config;
		private System.Windows.Forms.Label status;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtKey;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtValue;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox txtKey2;

		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtCountry = new System.Windows.Forms.TextBox();
			this.btnUpdate = new System.Windows.Forms.Button();
			this.status = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.txtKey = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtValue = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.label4 = new System.Windows.Forms.Label();
			this.txtKey2 = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "Country:";
			// 
			// txtCountry
			// 
			this.txtCountry.Location = new System.Drawing.Point(72, 24);
			this.txtCountry.Name = "txtCountry";
			this.txtCountry.Size = new System.Drawing.Size(176, 20);
			this.txtCountry.TabIndex = 1;
			this.txtCountry.Text = "";
			// 
			// btnUpdate
			// 
			this.btnUpdate.Location = new System.Drawing.Point(256, 24);
			this.btnUpdate.Name = "btnUpdate";
			this.btnUpdate.Size = new System.Drawing.Size(64, 20);
			this.btnUpdate.TabIndex = 2;
			this.btnUpdate.Text = "Update";
			this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
			// 
			// status
			// 
			this.status.Location = new System.Drawing.Point(40, 192);
			this.status.Name = "status";
			this.status.Size = new System.Drawing.Size(72, 16);
			this.status.TabIndex = 3;
			this.status.Visible = false;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 16);
			this.label2.TabIndex = 4;
			this.label2.Text = "Key Name:";
			// 
			// txtKey
			// 
			this.txtKey.Location = new System.Drawing.Point(72, 16);
			this.txtKey.Name = "txtKey";
			this.txtKey.Size = new System.Drawing.Size(96, 20);
			this.txtKey.TabIndex = 5;
			this.txtKey.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.btnUpdate);
			this.groupBox1.Controls.Add(this.txtCountry);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(16, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(400, 56);
			this.groupBox1.TabIndex = 6;
			this.groupBox1.TabStop = false;
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.button1);
			this.groupBox2.Controls.Add(this.txtValue);
			this.groupBox2.Controls.Add(this.label3);
			this.groupBox2.Controls.Add(this.txtKey);
			this.groupBox2.Controls.Add(this.label2);
			this.groupBox2.Location = new System.Drawing.Point(16, 72);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(440, 48);
			this.groupBox2.TabIndex = 7;
			this.groupBox2.TabStop = false;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(176, 16);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 16);
			this.label3.TabIndex = 6;
			this.label3.Text = "Key Value:";
			// 
			// txtValue
			// 
			this.txtValue.Location = new System.Drawing.Point(240, 16);
			this.txtValue.Name = "txtValue";
			this.txtValue.Size = new System.Drawing.Size(128, 20);
			this.txtValue.TabIndex = 7;
			this.txtValue.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(376, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(56, 20);
			this.button1.TabIndex = 8;
			this.button1.Text = "Insert";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.button2);
			this.groupBox3.Controls.Add(this.txtKey2);
			this.groupBox3.Controls.Add(this.label4);
			this.groupBox3.Location = new System.Drawing.Point(16, 128);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(432, 48);
			this.groupBox3.TabIndex = 8;
			this.groupBox3.TabStop = false;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 16);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(40, 16);
			this.label4.TabIndex = 0;
			this.label4.Text = "Key:";
			// 
			// txtKey2
			// 
			this.txtKey2.Location = new System.Drawing.Point(72, 16);
			this.txtKey2.Name = "txtKey2";
			this.txtKey2.Size = new System.Drawing.Size(144, 20);
			this.txtKey2.TabIndex = 1;
			this.txtKey2.Text = "";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(240, 16);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(56, 20);
			this.button2.TabIndex = 2;
			this.button2.Text = "Delete";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(464, 221);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.status);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			config = new Config();
			config.cfgFile = "app.config"; 
			txtCountry.Text = config.GetValue("//appSettings//add[@key='CountryLoc']");

			//txtCountry.Text = config.GetValue("//Company//add[@key='key1']");
		}

		private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			status.Visible = false;
			config.SetValue("//appSettings//add[@key='CountryLoc']", txtCountry.Text);
			//config.SetValue("//Company//add[@key='key1']", txtCountry.Text);

			status.Visible = true;
			status.Text= "Done!";
		}

		// insert key
		private void button1_Click(object sender, System.EventArgs e)
		{
			status.Visible = false;
			//config.SetValue("//Company//add[@key='" + txtKey.Text + "']", txtValue.Text);

			config.SetValue("//appSettings//add[@key='" + txtKey.Text + "']", txtValue.Text);

			status.Visible = true;
			status.Text= "Done!";
		}

		// delete
		private void button2_Click(object sender, System.EventArgs e)
		{
			status.Visible = false;
			//config.removeElement("//Company//add[@key='" + txtKey2.Text + "']");
			config.removeElement("//appSettings//add[@key='" + txtKey2.Text + "']");

			status.Visible = true;
			status.Text= "Done!";
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions