using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; //add sml namespace namespace XMLSettings { /****************************************** * This is the code for form1. It reads * the settings XML from file and uses * DOM and xpath to locate specific nodes * and apply them to the form properties * ***************************************/ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //on form load load the xml document and //apply the settings loadsettings(); } private void loadsettings() { XmlNodeList xnodes; //create a collection of nodes //create a new xml document XmlDocument doc = new XmlDocument(); try { //load the document from file doc.Load(@"..\..\Settings.xml"); } catch (Exception ex) { MessageBox.Show(ex.Message); } //use xpath to select a particular node xnodes = doc.DocumentElement.SelectNodes("//formtitle"); //loop through the selected nodes and replace //the setting with the inner text from the file //the loop is necessary even if there is only one //such node foreach (XmlNode xn in xnodes) { this.Text = xn.InnerText; } //all the rest follow the same pattern xnodes = doc.DocumentElement.SelectNodes("//formbackcolor"); foreach (XmlNode xn in xnodes) { this.BackColor = Color.FromName(xn.InnerText); } xnodes = doc.DocumentElement.SelectNodes("//fontcolor"); foreach (XmlNode xn in xnodes) { label1.ForeColor = Color.FromName(xn.InnerText); } xnodes = doc.DocumentElement.SelectNodes("//labeltext"); foreach (XmlNode xn in xnodes) { label1.Text = xn.InnerText; } doc = null; } private void button1_Click(object sender, EventArgs e) { //open the form to change setting ChangeSettings cs = new ChangeSettings(); cs.Show(); } } }